Crates.io | solink-tracing-flat-json |
lib.rs | solink-tracing-flat-json |
version | 1.0.0 |
source | src |
created_at | 2024-06-19 14:50:08.354833 |
updated_at | 2024-06-19 14:50:08.354833 |
description | Log flattened JSON in tracing. |
homepage | |
repository | |
max_upload_size | |
id | 1276969 |
size | 7,741 |
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}