Crate tracing_ndjson

source ·
Expand description

tracing-ndjson

Rust

A simple library for tracing in new-line delimited JSON format. This library is meant to be used with tracing as an alternative to the tracing_subscriber::fmt::json formatter.

Features

  • Configurable field names for target, message, level, and timestamp.
  • Configurable timestamp formats such as RFC3339, UNIX timestamp, or any custom chrono format.
  • Captures all span attributes and event fields in the root of the JSON object.

Usage

Add this to your Cargo.toml:

[dependencies]
tracing = "0.1"
tracing-ndjson = "0.1"
use tracing_subscriber::prelude::*;

tracing_subscriber::registry()
    .with(tracing_ndjson::builder().layer())
    .init();
tracing::info!(life = 42, "Hello, world!");
// {"level":"info","timestamp":"2023-10-08T03:30:52Z","target":"default","message":"Hello, world!"}
let span = tracing::info_span!("hello", "request.uri" = "https://example.com");
span.in_scope(|| {
    tracing::info!("Hello, world!");
    // {"level":"info","timestamp":"2023-10-08T03:34:33Z","target":"defaults","message":"Hello, world!","request.uri":"https://example.com"}
});

Examples

See the examples directory for more examples.

License

Licensed under MIT license LICENSE

Structs

  • A builder for the JSON formatter. This is used to configure the JSON formatter. The default configuration is:

Enums

  • A timestamp format for the JSON formatter. This is used to format the timestamp field in the JSON output. The default is RFC3339.

Functions

  • Alias for Builder::default(). This is used to configure the JSON formatter.
  • Returns a Layer that subscribes to all spans and events using a JSON formatter. This is used to configure the JSON formatter.