Struct Builder

Source
pub struct Builder<N = NewRecorder, E = Format, F = LevelFilter, W = fn() -> Stdout> { /* private fields */ }
Expand description

Configures and constructs Subscribers.

Implementations§

Source§

impl<N, E, F, W> Builder<N, E, F, W>
where N: for<'a> NewVisitor<'a> + 'static, E: FormatEvent<N> + 'static, W: MakeWriter + 'static, F: Layer<Formatter<N, E, W>> + 'static,

Source

pub fn finish(self) -> Subscriber<N, E, F, W>

Finish the builder, returning a new FmtSubscriber.

Source§

impl<N, E, F, W> Builder<N, E, F, W>
where N: for<'a> NewVisitor<'a> + 'static, E: FormatEvent<N> + 'static, W: MakeWriter + 'static, F: Layer<Formatter<N, E, W>> + 'static, Subscriber<N, E, F, W>: Send + Sync,

Source

pub fn try_init(self) -> Result<(), Box<dyn Error + Sync + Send>>

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.

Source

pub fn init(self)

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.

Source§

impl<N, L, T, F, W> Builder<N, Format<L, T>, F, W>
where N: for<'a> NewVisitor<'a> + 'static,

Source

pub fn with_timer<T2>(self, timer: T2) -> Builder<N, Format<L, T2>, F, W>

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.

Source

pub fn without_time(self) -> Builder<N, Format<L, ()>, F, W>

Do not emit timestamps with log messages.

Source

pub fn with_ansi(self, ansi: bool) -> Builder<N, Format<L, T>, F, W>

Enable ANSI encoding for formatted events.

Source

pub fn with_target(self, display_target: bool) -> Builder<N, Format<L, T>, F, W>

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

Source§

impl<N, E, W> Builder<N, E, EnvFilter, W>
where Formatter<N, E, W>: Subscriber + 'static,

Source

pub fn with_filter_reloading( self, ) -> Builder<N, E, Layer<EnvFilter, Formatter<N, E, W>>, W>

Configures the subscriber being built to allow filter reloading at runtime.

Source§

impl<N, E, W> Builder<N, E, Layer<EnvFilter, Formatter<N, E, W>>, W>
where Formatter<N, E, W>: Subscriber + 'static,

Source

pub fn reload_handle(&self) -> Handle<EnvFilter, Formatter<N, E, W>>

Returns a Handle that may be used to reload the constructed subscriber’s filter.

Source§

impl<N, E, F, W> Builder<N, E, F, W>

Source

pub fn with_visitor<N2>(self, new_visitor: N2) -> Builder<N2, E, F, W>
where N2: for<'a> NewVisitor<'a> + 'static,

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

Source

pub fn with_env_filter( self, filter: impl Into<EnvFilter>, ) -> Builder<N, E, EnvFilter, W>
where Formatter<N, E, W>: Subscriber + 'static,

Sets the EnvFilter that the subscriber will use to determine if a span or event is enabled.

Note that this method requires the “env-filter” feature flag to be enabled.

If a filter was previously set, or a maximum level was set by the with_max_level method, that value is replaced by the new filter.

§Examples

Setting a filter based on the value of the RUST_LOG environment variable:

use tracing_subscriber::{FmtSubscriber, EnvFilter};

let subscriber = FmtSubscriber::builder()
    .with_env_filter(EnvFilter::from_default_env())
    .finish();

Setting a filter based on a pre-set filter directive string:

use tracing_subscriber::FmtSubscriber;

let subscriber = FmtSubscriber::builder()
    .with_env_filter("my_crate=info,my_crate::my_mod=debug,[my_span]=trace")
    .finish();

Adding additional directives to a filter constructed from an env var:

use tracing_subscriber::{
    FmtSubscriber,
    filter::{EnvFilter, LevelFilter},
};

let filter = EnvFilter::try_from_env("MY_CUSTOM_FILTER_ENV_VAR")?
    // Set the base level when not matched by other directives to WARN.
    .add_directive(LevelFilter::WARN.into())
    // Set the max level for `my_crate::my_mod` to DEBUG, overriding
    // any directives parsed from the env variable.
    .add_directive("my_crate::my_mod=debug".parse()?);

let subscriber = FmtSubscriber::builder()
    .with_env_filter(filter)
    .finish();
Source

pub fn with_filter( self, filter: impl Into<EnvFilter>, ) -> Builder<N, E, EnvFilter, W>
where Formatter<N, E, W>: Subscriber + 'static,

👎Deprecated since 0.1.2: renamed to with_env_filter

Sets the EnvFilter that the subscriber will use to determine if a span or event is enabled.

Note: this method was renamed to with_env_filter in version 0.1.2. This method just wraps a call to with_env_filter, and will be removed in version 0.2.

Source

pub fn with_max_level( self, filter: impl Into<LevelFilter>, ) -> Builder<N, E, LevelFilter, W>

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::FmtSubscriber;
use tracing::Level;

let subscriber = FmtSubscriber::builder()
    .with_max_level(Level::DEBUG)
    .finish();

This subscriber won’t record any spans or events!

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

let subscriber = FmtSubscriber::builder()
    .with_max_level(LevelFilter::OFF)
    .finish();
Source

pub fn compact(self) -> Builder<N, Format<Compact>, F, W>
where N: for<'a> NewVisitor<'a> + 'static,

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

See format::Compact.

Source

pub fn on_event<E2>(self, fmt_event: E2) -> Builder<N, E2, F, W>
where E2: FormatEvent<N> + 'static,

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

Source

pub fn inherit_fields(self, inherit_fields: bool) -> Builder<N, E, F, W>

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

Source§

impl<N, E, F, W> Builder<N, E, F, W>

Source

pub fn with_writer<W2>(self, make_writer: W2) -> Builder<N, E, F, W2>
where W2: MakeWriter + 'static,

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

§Examples

Using stderr rather than stdout:

use std::io;

let subscriber = tracing_subscriber::fmt::Subscriber::builder()
    .with_writer(io::stderr)
    .finish();

Trait Implementations§

Source§

impl<N, E, F, W> Debug for Builder<N, E, F, W>
where N: Debug, E: Debug, F: Debug, W: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Builder

Source§

fn default() -> Builder

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

Auto Trait Implementations§

§

impl<N, E, F, W> Freeze for Builder<N, E, F, W>
where F: Freeze, N: Freeze, E: Freeze, W: Freeze,

§

impl<N, E, F, W> RefUnwindSafe for Builder<N, E, F, W>

§

impl<N, E, F, W> Send for Builder<N, E, F, W>
where F: Send, N: Send, E: Send, W: Send,

§

impl<N, E, F, W> Sync for Builder<N, E, F, W>
where F: Sync, N: Sync, E: Sync, W: Sync,

§

impl<N, E, F, W> Unpin for Builder<N, E, F, W>
where F: Unpin, N: Unpin, E: Unpin, W: Unpin,

§

impl<N, E, F, W> UnwindSafe for Builder<N, E, F, W>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Erased for T