tracing
Structured application-level tracing framework for Rust with spans, scoped fields, and contextual events.
Official Documentation ↗v0.1 core / v0.3 subscriber. Check the official docs for the latest API surface.Installation
cargo add tracing tracing-subscriberMethods
No methods match this filter.
Structured application-level tracing framework for Rust with spans, scoped fields, and contextual events.
Overview
tracing instruments Rust programs with structured, contextual events. Spans bracket units of work, requests, queries, tasks, and events inside a span inherit its fields, so log output carries the context that plain logging threads manually.
The subscriber ecosystem renders JSON or formatted text, samples high-rate spans, and bridges the log crate. Instrument attributes and manual spans cover most code, and EnvFilter gives per-crate verbosity control at runtime.
Core Methods
The most frequently used entry points, with signatures and runnable examples:
event macros— Emits structured events where fields are key-value pairs instead of interpolated text.#[tracing::instrument]— Wraps a function in a span carrying its name and arguments automatically.tracing_subscriber setup— Installs the global subscriber, with RUST_LOG-style level and target filtering.spans / instrument()— Creates spans programmatically and attaches them to futures so events nest inside.
Usage Notes
- Annotate request handlers with instrument once and every nested event gains context.
- Use %Display sigils for identifiers and ?Debug for complex structures deliberately.
- Ship JSON output in production so fields become queryable instead of greppable.
Common Pitfalls
- Skipping subscriber installation silently drops all events, the most common setup mistake.
- Holding spans across await points without instrument on the future loses the scope.
- Recording large structures by value moves them; prefer %sig or ?sig to borrow.
FAQ
- How does tracing relate to the log crate?
- tracing generalizes logging with context: events carry span scope, so a message knows which request or task produced it. The tracing-log bridge emits log records as events, letting old log-based crates join a tracing pipeline.
- Why do field values use % and ? sigils?
- %sig records Display, ?sig records Debug, and bare values record ownership-friendly primitives. Choosing the sigil controls cost: Display of a large structure is only paid when the event is enabled.
- How do I filter in production?
- EnvFilter parses RUST_LOG syntax, per-target levels and span-level filters, whileTargets defaults apply to everything unspecified. Set a sane default, error for noisy dependencies, and escalate only your own targets.
Report a problem with this API docOpens a pre-filled issue on GitHub.