Function bevy::log::tracing_subscriber::fmt

source ·
pub fn fmt() -> SubscriberBuilder
Expand description

Returns a new SubscriberBuilder for configuring a formatting subscriber.

This is essentially shorthand for [SubscriberBuilder::default()].

Examples

Using init to set the default subscriber:

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 subscriber as the default.
    .init();

try_init returns an error if the default subscriber could not be set:

use std::error::Error;

fn init_subscriber() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
    tracing_subscriber::fmt()
        // Configure the subscriber to emit logs in JSON format.
        .json()
        // Configure the subscriber to flatten event fields in the output JSON objects.
        .flatten_event(true)
        // Set the subscriber as the default, returning an error if this fails.
        .try_init()?;

    Ok(())
}

Rather than setting the subscriber as the default, finish returns the constructed subscriber, which may then be passed to other functions:

let subscriber = tracing_subscriber::fmt()
    .with_max_level(tracing::Level::DEBUG)
    .compact()
    .finish();

tracing::subscriber::with_default(subscriber, || {
    // the subscriber will only be set as the default
    // inside this closure...
})