Skip to main content

Crate tracing_microjson

Crate tracing_microjson 

Source
Expand description

A tracing JSON layer with zero serialization framework dependencies.

Drop-in replacement for tracing-subscriber’s json feature, producing identical output format without pulling in serde/serde_json/tracing-serde.

§Quick start

use tracing_microjson::JsonLayer;
use tracing_subscriber::prelude::*;

tracing_subscriber::registry()
    .with(JsonLayer::new(std::io::stderr))
    .init();

§Configuration

JsonLayer uses a builder pattern. All options have sensible defaults — only override what you need.

tracing_subscriber::registry()
    .with(
        JsonLayer::new(std::io::stderr)
            .with_target(false)
            .with_file(true)
            .with_line_number(true)
            .flatten_event(true)
            .without_time(),
    )
    .init();
MethodDefaultEffect
JsonLayer::with_targettrueInclude the event target (module path)
JsonLayer::with_filefalseInclude the source filename
JsonLayer::with_line_numberfalseInclude the source line number
JsonLayer::with_thread_idsfalseInclude the thread ID
JsonLayer::with_thread_namesfalseInclude the thread name
JsonLayer::flatten_eventfalseFlatten event fields to the top level instead of nesting under "fields"
JsonLayer::with_timerSystemTimestampUse a custom FormatTime implementation for timestamps
JsonLayer::without_timeDisable timestamps entirely
JsonLayer::with_buffer_capacity_limit4096Capacity threshold for per-thread buffer shrinking

§Output format

Every event is written as a single JSON line. The fields present depend on the configuration above and whether the event occurs inside a span:

{"timestamp":"…","level":"INFO","fields":{"message":"hello"},"target":"my_app","span":{"name":"req"},"spans":[{"name":"req"}]}
  • timestamp — RFC 3339 with microsecond precision in UTC by default. Customisable via with_timer or disabled with without_time.
  • level — always present (TRACE, DEBUG, INFO, WARN, ERROR).
  • fields — event fields, nested under "fields" by default. With flatten_event(true) they appear at the top level instead.
  • target — module path, present when with_target is true.
  • filename / line_number — source location, present when enabled via with_file / with_line_number.
  • threadId / threadName — thread info, present when enabled via with_thread_ids / with_thread_names.
  • span — the innermost active span (if any).
  • spans — all active spans from root to leaf (if any).

Structs§

JsonLayer
A tracing_subscriber::Layer that formats events as JSON lines.
SystemTimestamp
A timestamp formatter that produces RFC 3339 timestamps with microsecond precision in UTC (e.g. 2026-02-20T12:00:00.000000Z).

Traits§

FormatTime
A type that can measure and format the current time.