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.
FmtSubscriber is the subscriber that turns tracing events into lines of text. It is the part of tracing-subscriber you actually see, because its output is the log lines in your terminal. This page covers that subscriber in full: how to build it, what each builder method changes, and how the four output formats differ in real output.
Where This Fits
A subscriber receives spans and events and decides what to do with them. FmtSubscriber is the ready-made subscriber that formats them as text and writes them out. Every program on the earlier pages used it through the short form tracing_subscriber::fmt().init(). This page opens up that short form and shows every control it hides.
Think of it like a printer with settings. The subscriber is the printer. The builder is the settings panel. The default settings print clean lines with a timestamp, a level, a target, and a message. Each builder method changes one setting: hide the target, drop the timestamp, switch to JSON, or write to a file instead of the terminal.
The Smallest Working Version
This is the shortest program that shows FmtSubscriber by name instead of through the fmt() shortcut.
# Cargo.toml
[package]
name = "fmt_demo"
version = "0.1.0"
edition = "2021"
[dependencies]
tracing = "=0.1.44"
tracing-subscriber = "=0.3.23"
// src/main.rs
use tracing_subscriber::FmtSubscriber;
fn main() {
tracing::subscriber::set_global_default(FmtSubscriber::new()).unwrap();
tracing::info!("hello from FmtSubscriber");
}
Run it with cargo run. The terminal shows a line like this.
2026-11-14T11:47:31.680985Z INFO fmt_demo: hello from FmtSubscriber
FmtSubscriber::new() builds the subscriber with every default in place: full format, timestamps on, target shown, colours on when the terminal supports them, output to stdout, maximum level INFO. set_global_default installs it, and the unwrap says the program should stop if a subscriber is already installed. This form is useful when you want the subscriber value itself, but it is longer than the shortcut, so most programs use the builder instead.
The default line is the reference output:
If your output looks like the line above, with a timestamp, a level, a target, and a message, FmtSubscriber is working with its defaults. Every variation on this page is a change away from this one line, so keep it as the comparison point.
The Builder and the Shortcut Are the Same Thing
There are three ways to get a formatting subscriber, and they all end at the same builder type, SubscriberBuilder. Knowing they are the same thing stops a lot of confusion when you read other people's code.
The first way is the fmt() function.
// src/main.rs
fn main() {
tracing_subscriber::fmt().init();
tracing::info!("built with fmt()");
}
The second way is FmtSubscriber::builder(), which returns the same builder.
// src/main.rs
use tracing_subscriber::FmtSubscriber;
fn main() {
let subscriber = FmtSubscriber::builder()
.with_max_level(tracing::Level::DEBUG)
.finish();
tracing::subscriber::set_global_default(subscriber).unwrap();
tracing::debug!("built with FmtSubscriber::builder()");
}
The third way is FmtSubscriber::new(), which is the builder with no changes applied, finished at once. All three produce a FmtSubscriber. The difference is only how much configuration you add in between. Use fmt().init() in main when the defaults plus a level are enough. Use FmtSubscriber::builder() when you need the subscriber value before installing it. Use FmtSubscriber::new() when you want the defaults and nothing else, written out in full.
The builder methods all follow one pattern: each call takes the builder, changes one setting, and returns the builder, so you can chain them. The chain ends with either .init(), which installs the subscriber as the global default, or .finish(), which hands you the subscriber value to install yourself. A builder that is never finished or initialised does nothing at all. The subscriber only starts receiving events after one of those two calls.
A builder that is never finished installs nothing:
Writing tracing_subscriber::fmt().with_max_level(tracing::Level::DEBUG); on a line by itself, with no .init() or .finish() at the end, builds a builder and throws it away. Your program compiles, runs, and prints no logs. Always end the chain with .init() or .finish().
The Default Format Line by Line
The default format is called Full, and it prints one line per event. Here is a real line, produced by a program that logged a field along with its message.
2026-11-14T11:47:31.174710Z INFO formats: user signed in user_id=42
Reading it from left to right: the timestamp is in UTC with a Z at the end, which marks it as UTC time. Then comes the level, padded with spaces so that all levels line up in a column. INFO has two leading spaces in the output because ERROR is five letters wide and the formatter lines the levels up. Then the target, which is the module path where the event was emitted. For an event in a binary, that is the binary name. Then the message, and after the message the event fields, written as name=value pairs.
When the event happens inside a span, the span context appears between the target and the message.
2026-11-14T11:47:31.293914Z INFO checkout{user_id=42}: formats: payment received amount=99
The checkout{user_id=42} part is the span, with its name and its fields. It tells you which unit of work the event belonged to. The event fields, amount=99, still come after the message. Span fields and event fields look similar, but they sit in different places: span fields live inside the braces after the span name, and event fields live after the message.
The output goes to stdout, not stderr. That was checked by running a program with stdout and stderr sent to different places: the log lines came out on stdout, and stderr stayed empty. This matters when you redirect output in a script. cargo run > logs.txt captures the log lines, while cargo run 2> errors.txt does not.
Controlling the Maximum Level
The builder method .with_max_level() sets the most detailed level the subscriber will print. It takes a Level, and Level converts into a filter automatically, so you write the level name directly.
// src/main.rs
fn main() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
tracing::trace!("trace event");
tracing::debug!("debug event");
tracing::info!("info event");
tracing::warn!("warn event");
tracing::error!("error event");
}
With a maximum of DEBUG, four lines appear and the trace line is hidden.
2026-11-14T11:47:31.174654Z DEBUG levels: debug event
2026-11-14T11:47:31.174664Z INFO levels: info event
2026-11-14T11:47:31.174673Z WARN levels: warn event
2026-11-14T11:47:31.174681Z ERROR levels: error event
With a maximum of TRACE, all five appear. With a maximum of ERROR, only the error line appears. The maximum is a ceiling on detail: it lets through the named level and every level more urgent than it, and it blocks every level more detailed than it. The level names from most detailed to most urgent are TRACE, DEBUG, INFO, WARN, and ERROR, and what each one means is covered in its own full treatment later in this chapter.
You can also pass a LevelFilter instead of a Level. A LevelFilter is the same set of levels plus one extra value, OFF, which disables everything. Passing LevelFilter::OFF to with_max_level builds a subscriber that prints nothing at all.
// src/main.rs
use tracing_subscriber::filter::LevelFilter;
fn main() {
tracing_subscriber::fmt()
.with_max_level(LevelFilter::OFF)
.init();
tracing::error!("this line never appears");
}
Running this prints nothing. That sounds useless, but it is a real technique: a program can build its subscriber with OFF and then raise the level later through a filter, or use OFF in tests to silence output while checking that the code runs. The type lives in tracing_subscriber::filter, not in tracing, which is a detail the compiler will remind you of if you import it from the wrong place.
The Four Output Formats
The builder has four format methods, and each one changes the shape of every line. They are mutually exclusive: the last format call in the chain wins, because a subscriber can only print each event once, in one shape. The four shapes were all produced by running the same program with a different format call, so the differences below are real output, not descriptions.
Full, the default
The default needs no call at all. It prints one readable line per event, with the span context before the message.
// src/main.rs
fn main() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.init();
tracing::info!(user_id = 42, "user signed in");
tracing::debug!("debug line");
}
2026-11-14T11:47:31.174710Z INFO formats: user signed in user_id=42
2026-11-14T11:47:31.174768Z DEBUG formats: debug line
Use Full while you are developing. It is the easiest shape to read by eye, and it shows the timestamp, level, target, span, message, and fields in one line.
Compact, the short version
The .compact() call switches to a shorter form. For a plain event with no span, the output looks the same as Full. The difference appears when a span is active: Compact moves the span fields to the end of the line, after the event fields, instead of printing the span before the message.
// src/main.rs
fn main() {
tracing_subscriber::fmt()
.compact()
.with_max_level(tracing::Level::TRACE)
.init();
let span = tracing::info_span!("checkout", user_id = 42);
let _guard = span.enter();
tracing::info!(amount = 99, "payment received");
}
2026-11-14T11:47:31.602430Z INFO checkout: fmt2: payment received amount=99 user_id=42
Compare that with the Full version of the same event, which was INFO checkout{user_id=42}: formats: payment received amount=99. Compact keeps only the span name before the message and appends user_id=42 after the event fields. The line is shorter and easier to scan when spans carry many fields. Use Compact when your log lines are long and the span fields matter less than the message.
Pretty, the multi-line version
The .pretty() call prints each event across several lines, with the source file and line number included.
// src/main.rs
fn main() {
tracing_subscriber::fmt()
.pretty()
.with_max_level(tracing::Level::TRACE)
.init();
tracing::info!(user_id = 42, "user signed in");
tracing::debug!("debug line");
}
2026-11-14T11:47:31.195042Z INFO formats: user signed in, user_id: 42
at src/bin/formats.rs:19
2026-11-14T11:47:31.195096Z DEBUG formats: debug line
at src/bin/formats.rs:20
Each event takes three lines: the main line, an at line with the file and line number where the event was emitted, and a blank line. The fields are written with a colon, user_id: 42, instead of the equals sign used by Full. The file and line in the output above come from the test program that produced it; your own output shows your own file and line. Pretty is the best choice when you are debugging one confusing event and want to see exactly where it came from, and the worst choice for a busy server, because it multiplies the number of lines several times over.
JSON, the machine-readable version
The .json() call prints each event as one JSON object per line. It needs the json feature enabled in Cargo.toml.
[dependencies]
tracing-subscriber = { version = "0.3", features = ["json"] }
// src/main.rs
fn main() {
tracing_subscriber::fmt()
.json()
.with_max_level(tracing::Level::TRACE)
.init();
tracing::info!(user_id = 42, "user signed in");
tracing::debug!("debug line");
}
{"timestamp":"2026-11-14T11:47:31.204606Z","level":"INFO","fields":{"message":"user signed in","user_id":42},"target":"formats"}
{"timestamp":"2026-11-14T11:47:31.204695Z","level":"DEBUG","fields":{"message":"debug line"},"target":"formats"}
Each line is a complete JSON object with the timestamp, the level, the fields object holding the message and any extra fields, and the target. Numbers stay numbers: user_id is 42 without quotes. When a span is active, the object gains a span key and a spans list holding the span fields, so a log collection tool can search by span field without parsing text. JSON is the right choice when logs go to a service that reads them, and the wrong choice when a human reads them in a terminal, because the lines are long and hard to scan.
Two JSON-only builder methods adjust the shape. .flatten_event(true) lifts the message and fields out of the fields object to the top level of the JSON object.
{"timestamp":"2026-11-14T11:47:31.673600Z","level":"INFO","message":"user signed in","user_id":42,"target":"fmt2"}
.with_current_span(false) drops the single span object from the output while keeping the spans list. Both were verified by running the program. The full JSON treatment, including how log collection tools use these fields, belongs to the structured output page later in this chapter.
The format and the filter are separate settings:
Changing the format never changes which events appear. A JSON subscriber with a maximum of INFO hides DEBUG events exactly like a Full subscriber does, and a Pretty subscriber with a maximum of TRACE shows them all. If lines are missing, the problem is the level or the filter, never the format call.
Hiding and Showing Parts of a Line
Four builder methods toggle individual pieces of the Full format. Each takes a bool, so you can turn a piece off without changing anything else.
.with_target(false) hides the target, which is the module path after the level.
// src/main.rs
fn main() {
tracing_subscriber::fmt()
.with_target(false)
.with_max_level(tracing::Level::TRACE)
.init();
tracing::info!(user_id = 42, "user signed in");
}
2026-11-14T11:47:31.206963Z INFO user signed in user_id=42
The target is useful when many crates log at once and you need to tell them apart, and it is noise when only your own crate logs. Turn it off for single-crate programs and leave it on for servers with dependencies that log.
.with_level(false) hides the level. This is rarely a good idea, because the level is the fastest way to tell an error from a routine message, but the method exists for output that goes somewhere the level is already recorded.
.with_file(true) and .with_line_number(true) add the source file and line number to each line, in the same style Pretty uses, but without the multi-line layout.
2026-11-14T11:47:31.510707Z INFO fmt2: src/bin/fmt2.rs:14: with file and line
The file path is relative to the crate root as the compiler sees it. Turn these on when you are tracking down where a message comes from, and turn them off again for normal running, because they make every line longer.
.with_thread_names(true) and .with_thread_ids(true) add the current thread to each line.
2026-11-14T11:47:31.579905Z INFO main ThreadId(01) fmt2: with thread info
The thread name is main for the main thread. Worker threads in a server get names from the runtime or fall back to their IDs. These two methods earn their place in a multi-threaded server, where two requests on two threads can otherwise produce interleaved lines with no way to tell them apart.
Timestamps and Colours
.with_timer(...) replaces the timestamp. The default timer prints the current UTC time. The fmt::time module provides alternatives, and the most useful one for short runs is uptime, which prints the time since the program started instead of the wall-clock time.
// src/main.rs
fn main() {
tracing_subscriber::fmt()
.with_timer(tracing_subscriber::fmt::time::uptime())
.with_max_level(tracing::Level::TRACE)
.init();
tracing::info!("uptime timer");
}
0.000221443s INFO fmt2: uptime timer
The timestamp reads as a duration in seconds since startup, which makes it easy to see how long after launch each event happened. .without_time() removes the timestamp entirely, which is useful when the program output is already timestamped by something else, such as a container runtime that adds its own timestamps to every line.
.with_ansi(true) turns on colour codes, and .with_ansi(false) turns them off. Colours need the ansi feature, which is on by default. In a terminal, the level is coloured: errors stand out in red, warnings in yellow, and the rest in plain text. When output is piped to a file, the colour codes become visible escape sequences in the file, which is why you turn ANSI off for file output. The formatter detects a non-terminal on its own in many cases, but an explicit .with_ansi(false) is the reliable choice when you write to a file.
Span Events
By default, a span opening and closing prints nothing by itself. Only the events inside the span print, with the span context attached. .with_span_events(...) changes that, and it takes a FmtSpan value that says which span moments to log.
// src/main.rs
use tracing_subscriber::fmt::format::FmtSpan;
fn main() {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
.with_span_events(FmtSpan::CLOSE)
.init();
let span = tracing::info_span!("work", job = "backup");
let _guard = span.enter();
tracing::info!("inside span");
}
Running this prints two lines.
2026-11-14T11:47:31.506341Z INFO work{job="backup"}: fmt2: inside span
2026-11-14T11:47:31.506501Z INFO work{job="backup"}: fmt2: close time.busy=172 microseconds time.idle=33.8 microseconds
The second line is the close event, printed when the span guard drops at the end of main. It reports time.busy, the time the span was actually entered, and time.idle, the time the span existed but was not entered. The exact numbers vary from run to run. On a real terminal the unit is shortened to the Greek letter mu, so the numbers read 172 and 33.8 followed by that symbol; the page spells the unit out so the text stays plain ASCII. FmtSpan also offers NEW, which logs when a span is created, ENTER and EXIT, which log each entry and exit, and FULL, which logs all of them. Span events are how you measure how long a unit of work took without adding a timer by hand, but they double the number of lines, so turn them on for the spans you are measuring and leave them off everywhere else.
Writing Somewhere Other Than the Terminal
.with_writer(...) changes where the formatted lines go. It takes anything that can produce a writer, and the two useful choices are a file and stderr.
Writing to a file looks like this.
// src/main.rs
fn main() {
let file = std::fs::File::create("app.log").unwrap();
tracing_subscriber::fmt()
.with_writer(file)
.with_max_level(tracing::Level::TRACE)
.init();
tracing::info!("this goes to the file");
println!("stdout line only");
}
Running this prints only stdout line only in the terminal. The log line lands in app.log, and reading that file shows the formatted line. The file is created when the program starts, and each run overwrites it, because File::create truncates an existing file. For a real server you would open the file in append mode instead, so restarts do not erase earlier logs.
Writing to stderr looks like this.
// src/main.rs
fn main() {
tracing_subscriber::fmt()
.with_writer(std::io::stderr)
.with_max_level(tracing::Level::TRACE)
.init();
tracing::info!("this goes to stderr");
}
Running it with stdout and stderr separated shows the line on stderr and nothing on stdout. Stderr is the right destination when stdout is reserved for the program real output, such as a command-line tool that prints data for another program to read. Logs on stdout would corrupt that data, while logs on stderr stay separate.
with_writer replaces the output, it does not add a second one:
Calling .with_writer(file) means the terminal gets nothing. If you want both the terminal and a file, one subscriber is not enough. You build a registry with two formatting layers, one writing to each place, which is the composition pattern from the subscriber page.
A Custom Event Format
When the toggle methods are not enough, .event_format(...) takes a fully configured formatter built with fmt::format(). This is how you change several pieces at once in one place.
// src/main.rs
use tracing_subscriber::fmt;
fn main() {
let format = fmt::format()
.with_level(false)
.with_target(false)
.with_thread_ids(true)
.with_thread_names(true)
.compact();
tracing_subscriber::fmt()
.event_format(format)
.init();
tracing::info!("custom format running");
}
The fmt::format() call builds a Format value, the chained calls set its options, and .compact() picks the Compact style as the base. The .event_format(format) call hands that formatter to the subscriber builder. The order matters: call .event_format(...) instead of .compact() or .json() on the builder, because those builder methods set the format themselves and would replace the custom one. This form is the escape hatch for output shapes the toggles cannot express, and most programs never need it.
Common Beginner Mistakes
Calling the format method after init
// Wrong - init ends the chain
tracing_subscriber::fmt().init().compact();
.init() consumes the builder and installs the subscriber. Nothing can be chained after it. All configuration calls go before .init(), in the same chain.
Enabling JSON without the feature
// Wrong without features = ["json"] - will not compile
tracing_subscriber::fmt().json().init();
The compiler reports that no method named json exists. The fix is the feature flag in Cargo.toml, tracing-subscriber = { version = "0.3", features = ["json"] }.
Expecting compact to change which lines appear
Compact changes the shape of the lines, not the set of lines. If a DEBUG line is missing under Compact, the maximum level is still INFO. Change the level, not the format.
Writing logs to a file and wondering where the terminal output went
.with_writer(file) moves all output to the file. The terminal stays empty because there is nothing left to print there. Check the file before concluding the subscriber is broken.
How the Builder Methods Fit Together
The builder settings fall into four groups, and each group answers one question about the output.
which events -> with_max_level, with_env_filter
what shape -> compact, pretty, json, event_format
which parts -> with_target, with_level, with_file, with_thread_names
where it goes -> with_writer, with_ansi, with_timer
The first group decides what gets printed. The second decides the shape. The third decides the pieces inside the shape. The fourth decides the destination. When output looks wrong, find the group that owns the wrong part and change only that group. A missing line is a first-group problem. A line in the wrong shape is a second-group problem. A line missing its target is a third-group problem. A line in the wrong place is a fourth-group problem.
Testing the Builder
Builder configuration is easy to test because building a subscriber does not install it. This test file checks that the builder forms used on this page compile and produce a subscriber value, which catches a missing feature flag or a wrong method name at test time instead of at runtime.
// tests/fmt_tests.rs
#[test]
fn builder_with_debug_level_builds() {
let _subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::DEBUG)
.finish();
}
#[test]
fn json_builder_builds() {
let _subscriber = tracing_subscriber::fmt()
.json()
.with_max_level(tracing::Level::DEBUG)
.finish();
}
#[test]
fn compact_builder_with_writer_builds() {
let _subscriber = tracing_subscriber::fmt()
.compact()
.with_writer(|| std::io::sink())
.finish();
}
Run the tests with cargo test. All three pass. The closure returning std::io::sink() in the third test throws output away, which is what you want in a test: it proves the builder chain compiles without writing a file. Note that the JSON test needs the json feature enabled in Cargo.toml, or it fails to compile with the missing-method error from the mistakes section above.
Final Complete Example
Here is a program that uses the builder the way a real service would: JSON output for a log collector, a DEBUG maximum, no ANSI colours because the output goes to a file, and a file writer.
# Cargo.toml
[package]
name = "fmt_demo"
version = "0.1.0"
edition = "2021"
[dependencies]
tracing = "=0.1.44"
tracing-subscriber = { version = "=0.3.23", features = ["json"] }
// src/main.rs
fn main() {
let file = std::fs::File::create("app.log").unwrap();
tracing_subscriber::fmt()
.json()
.with_max_level(tracing::Level::DEBUG)
.with_ansi(false)
.with_writer(file)
.init();
tracing::debug!("debug line for the collector");
let span = tracing::info_span!("checkout", user_id = 42);
let _guard = span.enter();
tracing::info!(amount = 99, "payment received");
}
Running it prints nothing to the terminal. Reading app.log shows two JSON lines, one for the debug event and one for the info event inside the span, with the span fields carried in the JSON object. That is the whole page in one program: the builder picks the shape, the level picks the events, the writer picks the destination, and the span adds its context to the lines that need it.
Summary
FmtSubscriber is one subscriber with a large settings panel, and the panel splits into four questions: which events, what shape, which parts, and where the output goes. The defaults answer those questions with INFO, Full, everything shown, and stdout, which is the right starting point for development. From there, each change is one method call, and each method call has a visible effect on the very next run, so the fastest way to learn the builder is to change one setting at a time and read the output. The choice that matters most in practice is not between Full and Compact. It is between a format a human reads and a format a machine reads, because that choice decides who consumes the logs for the life of the service. Pick the human format while you are building, pick JSON when a collector starts reading the logs, and keep the level and the filter separate from both, so changing the shape never accidentally changes what gets recorded.