Observability - Axum - Complete Documentation by ntxm.org

The tracing crate

Learn what the tracing crate is, how its events and levels work, and how to add the first structured log lines to a Rust program

Tracing spans

Learn what a tracing span is, how to create and enter spans, how nested spans build a tree, and how spans attach context to events in Rust

Tracing events

Learn what a tracing event is, how to record events with fields and messages, how the five level macros work, and how events inherit span context

The instrument attribute

Learn how the tracing instrument attribute adds a span to a function automatically, how to skip fields and add custom fields, and how to use it on async functions and Axum handlers

tracing-subscriber - the crate that makes your logs visible

Learn what tracing-subscriber is, how a subscriber collects spans and events, how to install one global default, and why logs vanish without it.

FmtSubscriber - the formatting subscriber and its builder

Learn how FmtSubscriber turns tracing events into readable log lines, how to configure it with the builder, and how to switch between full, compact, pretty, and JSON output.

EnvFilter - filtering logs by target and level

Learn how EnvFilter reads directive strings, how target and level rules combine, how span filters work, and how to build a filter from code or the environment.

RUST_LOG - controlling log levels from the environment

Learn what the RUST_LOG environment variable does, how tracing reads it at startup, which values to use in development and production, and what happens when it is missing or misspelled.

Tracing log levels - TRACE DEBUG INFO WARN and ERROR

Learn the five tracing levels, how they order by verbosity, how Level and LevelFilter differ, and how to choose the right level for every message.

HTTP TraceLayer

Learn how TraceLayer adds request tracing to an Axum server, what it logs for each request, and how to set it up

HTTP Request Spans

Learn how TraceLayer builds one span per HTTP request, what fields the span carries, and how to shape it with make_span_with

HTTP Latency Fields

Learn where the latency field in TraceLayer logs comes from, what unit it uses, and how to change it with LatencyUnit

HTTP Status Fields

Learn how TraceLayer records the response status code, what counts as a failure, and how to put the status on the request span

Structured JSON log output

Learn how to switch tracing output to structured JSON lines with tracing-subscriber, read every key in a JSON log line, and shape the output for log search tools

Correlating logs with request ID

Learn how to carry a request id into every log line of one request with tracing spans, follow one request through JSON logs, and keep two requests apart

trace debug info warn error levels

Learn the five tracing levels TRACE DEBUG INFO WARN and ERROR, what each one is for, how filtering hides the lower ones, and how to pick the right level for every log line