Getting started with the tracing crate in Rust: spans, events and tokio-console

tracing is the Tokio project’s library for structured, contextual diagnostics.
Where a plain logger records isolated lines, tracing records spans (a period of time, like a function
call or a request) and events (a point in time inside a span), so every log line knows which request or
task it belongs to. You instrument your code once, then choose at startup what to do with the data:
print it, filter it, time it, or ship it to another tool.
tracing provides the macros you put in your code; a subscriber decides what happens to the data.
tracing-subscriber provides the usual one.
[dependencies]tracing = "0.1"tracing-subscriber = "0.3"(The crate name uses a hyphen; you refer to it as tracing_subscriber in code. This post was checked against
tracing 0.1.44 and tracing-subscriber 0.3.23.)
use tracing::{info, instrument};
fn main() { // Install a global subscriber that prints events to stdout. tracing_subscriber::fmt::init();
some_function(42);}
#[instrument]fn some_function(id: u32) { info!("doing some work");}fmt::init() installs a formatting subscriber globally (it panics if one is already installed). With the
default features it prints events at INFO and above; if you enable the env-filter feature it reads the
filter from RUST_LOG instead, e.g. RUST_LOG=debug cargo run.
#[instrument] wraps the function body in a span named after the function and records its arguments as
fields using their Debug impls, so the event above prints with some_function{id=42} as context. Use
#[instrument(skip(big_arg))] for arguments that are large or don’t implement Debug.
Spans and events
Section titled “Spans and events”- A span has a start and end, can carry fields, and nests inside other spans.
- An event is a single point in time. It is recorded inside whatever span is current.
Create a span manually with span! (or the level shortcuts info_span!, debug_span!, …) and enter it:
use tracing::{info, info_span};
fn main() { tracing_subscriber::fmt::init();
let span = info_span!("load_config", path = "app.toml"); let _guard = span.enter();
info!(entries = 3, "config loaded"); // recorded inside load_config} // span exited when _guard dropsEvents take structured fields (entries = 3) in addition to the message, which is what makes the output
filterable and machine-readable (tracing_subscriber::fmt().json() with the json feature).
Timing spans
Section titled “Timing spans”For quick performance checks you don’t need extra tools. Ask the fmt subscriber to log when spans close;
it adds time.busy (time spent inside the span) and time.idle (time the span existed but wasn’t entered):
use tracing::instrument;use tracing_subscriber::fmt::format::FmtSpan;
fn main() { tracing_subscriber::fmt() .with_span_events(FmtSpan::CLOSE) .init();
slow();}
#[instrument]fn slow() { std::thread::sleep(std::time::Duration::from_millis(200));}The idle time is mostly useful for async code, where it shows how long a future spent waiting rather
than running.
Inspecting async tasks with tokio-console
Section titled “Inspecting async tasks with tokio-console”For Tokio applications, tokio-console is a terminal UI (“top for tasks”) that shows every task, how long it has been busy or idle, and how often it has been polled. It needs three things:
-
The
console-subscribercrate (0.5.0 at the time of writing) in your app:[dependencies]console-subscriber = "0.5"tokio = { version = "1", features = ["full", "tracing"] } -
A call to
console_subscriber::init()at the top ofmain(instead oftracing_subscriber::fmt::init()):#[tokio::main]async fn main() {console_subscriber::init();// your application code} -
Tokio’s unstable instrumentation turned on at build time, either with
RUSTFLAGS="--cfg tokio_unstable" cargo runor in.cargo/config.toml:[build]rustflags = ["--cfg", "tokio_unstable"]
Then install and run the console in another terminal. It connects to http://127.0.0.1:6669 by default:
cargo install --locked tokio-consoletokio-consoleFurther reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)