logo

Struct tracing_subscriber::fmt::SubscriberBuilder[][src]

pub struct SubscriberBuilder<N = DefaultFields, E = Format<Full>, F = LevelFilter, W = fn() -> Stdout> { /* fields omitted */ }
Expand description

Configures and constructs Subscribers.

Implementations

Finish the builder, returning a new FmtSubscriber.

Install this Subscriber as the global default if one is not already set.

If the tracing-log feature is enabled, this will also install the LogTracer to convert Log records into tracing Events.

Errors

Returns an Error if the initialization was unsuccessful, likely because a global subscriber was already installed by another call to try_init.

Install this Subscriber as the global default.

If the tracing-log feature is enabled, this will also install the LogTracer to convert Log records into tracing Events.

Panics

Panics if the initialization was unsuccessful, likely because a global subscriber was already installed by another call to try_init.

Use the given timer for log message timestamps.

See time for the provided timer implementations.

Note that using the chrono feature flag enables the additional time formatters ChronoUtc and ChronoLocal.

Do not emit timestamps with log messages.

Configures how synthesized events are emitted at points in the span lifecycle.

The following options are available:

  • FmtSpan::NONE: No events will be synthesized when spans are created, entered, exited, or closed. Data from spans will still be included as the context for formatted events. This is the default.
  • FmtSpan::NEW: An event will be synthesized when spans are created.
  • FmtSpan::ENTER: An event will be synthesized when spans are entered.
  • FmtSpan::EXIT: An event will be synthesized when spans are exited.
  • FmtSpan::CLOSE: An event will be synthesized when a span closes. If timestamps are enabled for this formatter, the generated event will contain fields with the span’s busy time (the total time for which it was entered) and idle time (the total time that the span existed but was not entered).
  • FmtSpan::ACTIVE: An event will be synthesized when spans are entered or exited.
  • FmtSpan::FULL: Events will be synthesized whenever a span is created, entered, exited, or closed. If timestamps are enabled, the close event will contain the span’s busy and idle time, as described above.

The options can be enabled in any combination. For instance, the following will synthesize events whenever spans are created and closed:

use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt;

let subscriber = fmt()
    .with_span_events(FmtSpan::NEW | FmtSpan::CLOSE)
    .finish();

Note that the generated events will only be part of the log output by this formatter; they will not be recorded by other Subscribers or by Layers added to this subscriber.

Sets whether or not an event’s target is displayed.

Sets whether or not an event’s level is displayed.

Sets whether or not the name of the current thread is displayed when formatting events

Sets whether or not the thread ID of the current thread is displayed when formatting events

Sets the subscriber being built to use a less verbose formatter.

See format::Compact.

Sets the Visitor that the subscriber being built will use to record fields.

For example:

use tracing_subscriber::fmt::format;
use tracing_subscriber::prelude::*;

let formatter =
    // Construct a custom formatter for `Debug` fields
    format::debug_fn(|writer, field, value| write!(writer, "{}: {:?}", field, value))
        // Use the `tracing_subscriber::MakeFmtExt` trait to wrap the
        // formatter so that a delimiter is added between fields.
        .delimited(", ");

let subscriber = tracing_subscriber::fmt()
    .fmt_fields(formatter)
    .finish();

Sets the maximum verbosity level that will be enabled by the subscriber.

If the max level has already been set, or a EnvFilter was added by with_filter, this replaces that configuration with the new maximum level.

Examples

Enable up to the DEBUG verbosity level:

use tracing_subscriber::fmt;
use tracing::Level;

fmt()
    .with_max_level(Level::DEBUG)
    .init();

This subscriber won’t record any spans or events!

use tracing_subscriber::{fmt, filter::LevelFilter};

let subscriber = fmt()
    .with_max_level(LevelFilter::OFF)
    .finish();

Sets the function that the subscriber being built should use to format events that occur.

👎 Deprecated since 0.2.0:

this no longer does anything

Sets whether or not spans inherit their parents’ field values (disabled by default).

👎 Deprecated since 0.2.0:

renamed to event_format.

Sets the function that the subscriber being built should use to format events that occur.

Sets the MakeWriter that the subscriber being built will use to write events.

Examples

Using stderr rather than stdout:

use tracing_subscriber::fmt;
use std::io;

fmt()
    .with_writer(io::stderr)
    .init();

Configures the subscriber to support libtest’s output capturing when used in unit tests.

See TestWriter for additional details.

Examples

Using TestWriter to let cargo test capture test output. Note that we do not install it globally as it may cause conflicts.

use tracing_subscriber::fmt;
use tracing::subscriber;

subscriber::set_default(
    fmt()
        .with_test_writer()
        .finish()
);

Trait Implementations

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Sets self as the default subscriber in the current scope, returning a guard that will unset it when dropped. Read more

Attempts to set self as the global default subscriber in the current scope, returning an error if one is already set. Read more

Attempts to set self as the global default subscriber in the current scope, panicking if this fails. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.