Function tracing_subscriber::fmt::fmt

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

Returns a new SubscriberBuilder for configuring a formatting subscriber.

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


Using init to set the default subscriber:


Configuring the output format:

    // Configure formatting settings.
    // Set the subscriber as the default.

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>> {
        // Configure the subscriber to emit logs in JSON format.
        // Configure the subscriber to flatten event fields in the output JSON objects.
        // Set the subscriber as the default, returning an error if this fails.


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()

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