solink-tracing-flat-json

Crates.iosolink-tracing-flat-json
lib.rssolink-tracing-flat-json
version1.0.0
sourcesrc
created_at2024-06-19 14:50:08.354833
updated_at2024-06-19 14:50:08.354833
descriptionLog flattened JSON in tracing.
homepage
repository
max_upload_size
id1276969
size7,741
Jason Walton (jwalton)

documentation

README

solink-tracing-flat-json

This is an open source library that can be used with tracing to log as flattened JSON:

    use solink_tracing_flat_json::SolinkJsonFormat;

    let log_to_file = tracing_subscriber::fmt::layer()
        .event_format(
            SolinkJsonFormat::new()
        )
        .fmt_fields(JsonFields::default());
    tracing_subscriber::registry().with(log_to_file).init();

This will serialize a timestamp, all variables in the event, the name of the current span, and all variables in the current and all parent spans.

An example like this:

#[tokio::main(flavor = "current_thread")]
async fn main() {
    a(42).await;
}

#[instrument]
async fn a(x: u64) {
    b(x).await;
}

#[instrument]
async fn b(y: u64) {
    info!(z = 94, "Hello from b")
}

Will produce output like:

{"timestamp":"2024-06-18T21:17:44.902137000Z","level":"INFO","message":"Hello from b","z":94,"span":"b","y":42,"x":42}
Commit count: 0

cargo fmt