json-subscriber
Customizable JSON logging layer and subscriber for Rust's tracing ecosystem
Repository Health
Technical Analysis
json-subscriber is a Rust crate that acts as a near drop-in replacement for tracing_subscriber’s JSON formatter, letting you emit structured JSON log lines with minimal changes to existing tracing setup. It goes further than the standard formatter by letting you shape the structure of each log line, inject custom static and dynamic fields, serialize extensions provided by other layers, and include OpenTelemetry trace and span IDs.
Built to be as compatible as possible with tracing_subscriber’s fmt API, most existing configuration works unchanged, while the crate focuses solely on JSON output and aims to be faster than the standard subscriber for that use case.
What You Get
- A drop-in JSON formatter compatible with tracing_subscriber’s fmt() and Layer builder APIs
- Customizable log-line structure with static and dynamic user-defined fields
- Optional OpenTelemetry trace ID and span ID injection into every log line
- Serialization of extensions attached by other tracing layers
- Feature-flagged support for a wide range of tracing-opentelemetry versions
Common Use Cases
- Emitting JSON-formatted application logs for ingestion by log aggregators
- Migrating from tracing_subscriber’s JSON output while gaining customization
- Correlating logs with distributed traces via OpenTelemetry span and trace IDs
- Adding service-specific static metadata to every structured log line
Under The Hood
Architecture - The crate is organized around a JsonLayer type (src/layer/) that implements tracing_subscriber’s Layer trait, with a fmt module (src/fmt/) providing the compatible builder and init helpers, a serde module bridging tracing’s field visitors to serde serialization, and supporting primitives like a custom cursor, cached values, and field writers that assemble each JSON line. Tech Stack - Written in pure Rust (edition 2021, MSRV 1.75), it builds on tracing, tracing-subscriber, and serde/serde_json, with extensive feature flags wiring up many tracing-opentelemetry and opentelemetry version pairs so downstream projects can match their existing OpenTelemetry stack. Code Quality - The repository includes an in-crate test module (src/tests.rs), multiple runnable examples, Criterion-style benchmarks under benches/, a CHANGELOG, rustfmt and cargo-deny configuration, and CI, indicating a maintained and disciplined codebase. API Design - The public API deliberately mirrors tracing_subscriber’s fmt() and layer() builders so adoption is a near one-line change, with expressive methods like with_opentelemetry_ids and serialize_extension for customization, keeping boilerplate minimal for the common case.