Best way to log with JSON

I was wondering what the best practices are for logging JSON data with rust? I'm building an application that's gathering logs from several other applications' APIs and then shipping the logs to elastisearch. I was going to use fluentbit or vector.dev to ship the logs from this application.

Being able to use JSON will make it easier to tag those logs.

I tried using JSON-env-logher but it seems unmaintained and I get this issue. the trait `ToValue` is not implemented for `std::string::String` · Issue #6 · softprops/json-env-logger · GitHub

What's the simplest/best method for making logging output json

1 Like

The tracing framework has a way to format logs in json. You might get started with something like:

fn main() {
    tracing_subscriber::fmt().json().init();
    log::info!("hello");
}

Make sure to enable the json feature in Cargo.toml:

tracing-subscriber = { version = "0.3.16", features = ["json"] }
3 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.