solink-tracing-flat-json

Log flattened JSON in tracing

1 stable release

1.0.0 Jun 19, 2024
Download history 51/week @ 2024-07-01 1/week @ 2024-07-08 13/week @ 2024-07-15 4/week @ 2024-07-22 26/week @ 2024-07-29 45/week @ 2024-08-12 25/week @ 2024-09-09 41/week @ 2024-09-16 66/week @ 2024-09-23 66/week @ 2024-09-30 16/week @ 2024-10-07 14/week @ 2024-10-14

164 downloads per month

MIT license

8KB
146 lines

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}

Dependencies

~4–5.5MB
~95K SLoC