pub fn fmt() -> CollectorBuilder
Available on crate features
fmt
and std
only.Expand description
Returns a new CollectorBuilder
for configuring a formatting collector.
This is essentially shorthand for CollectorBuilder::default()
.
ยงExamples
Using init
to set the default collector:
tracing_subscriber::fmt().init();
Configuring the output format:
tracing_subscriber::fmt()
// Configure formatting settings.
.with_target(false)
.with_timer(tracing_subscriber::fmt::time::uptime())
.with_level(true)
// Set the collector as the default.
.init();
try_init
returns an error if the default collector could not be set:
use std::error::Error;
fn init_subscriber() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
tracing_subscriber::fmt()
// Configure the collector to emit logs in JSON format.
.json()
// Configure the collector to flatten event fields in the output JSON objects.
.flatten_event(true)
// Set the collector as the default, returning an error if this fails.
.try_init()?;
Ok(())
}
Rather than setting the collector as the default, finish
returns the
constructed collector, which may then be passed to other functions:
let collector = tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.compact()
.finish();
tracing::collect::with_default(collector, || {
// the collector will only be set as the default
// inside this closure...
})