tracing_json_span_fields

Crates.iotracing_json_span_fields
lib.rstracing_json_span_fields
version0.1.1
sourcesrc
created_at2023-07-25 11:20:46.446714
updated_at2023-07-25 16:28:41.375087
descriptionStructured JSON logging from tracing with fields from spans
homepage
repositoryhttps://github.com/jjtt/tracing_json
max_upload_size
id925460
size29,071
(jjtt)

documentation

README

Structured JSON logging from tracing with fields from spans

Unlike the JSON support in tracing_subscriber, this implementation treats spans as a way to provide context and adds all fields from all spans to the logged events.

Examples

use tracing::{info, info_span};
use tracing_subscriber::prelude::*;
use tracing_json_span_fields::JsonLayer;
tracing_subscriber::registry().with(JsonLayer::pretty()).init();
let _span = info_span!("A span", span_field = 42).entered();
info!(logged_message_field = "value", "Logged message");

Will produce the following output

{
  "log_level": "INFO",
  "logged_message_field": "value",
  "message": "Logged message",
  "name": "event src/main.rs:123",
  "span_field": 42,
  "target": "tracing_json",
  "timestamp": "2023-07-25T09:53:01.790152227Z"
}

Customising timestamps

use time::macros::format_description;
use tracing::{error, info_span};
use tracing_subscriber::prelude::*;
use tracing_json_span_fields::JsonLayer;
let timestamp_format = format_description!("[hour]:[minute]:[second].[subsecond digits:1]");
tracing_subscriber::registry().with(JsonLayer::default().with_timestamp_format(timestamp_format)).init();
let _span = info_span!("A span", span_field = 42).entered();
error!(logged_message_field = "value", "Logged message");

Will produce the following output

{"log_level":"ERROR","logged_message_field":"value","message":"Logged message","name":"event src/main.rs:123","span_field":42,"target":"tracing_json","timestamp":"10:02:01.9"}

Thanks

See also

Commit count: 25

cargo fmt