pub struct CollectorBuilder<N = DefaultFields, E = Format, F = LevelFilter, W = fn() -> Stdout> { /* private fields */ }
fmt
and std
only.Expand description
Configures and constructs Collector
s.
Implementations§
Source§impl<N, E, F, W> CollectorBuilder<N, E, F, W>where
N: for<'writer> FormatFields<'writer> + 'static,
E: FormatEvent<Registry, N> + 'static,
W: for<'writer> MakeWriter<'writer> + 'static,
F: Subscribe<Formatter<N, E, W>> + Send + Sync + 'static,
Subscriber<Registry, N, E, W>: Subscribe<Registry> + Send + Sync + 'static,
impl<N, E, F, W> CollectorBuilder<N, E, F, W>where
N: for<'writer> FormatFields<'writer> + 'static,
E: FormatEvent<Registry, N> + 'static,
W: for<'writer> MakeWriter<'writer> + 'static,
F: Subscribe<Formatter<N, E, W>> + Send + Sync + 'static,
Subscriber<Registry, N, E, W>: Subscribe<Registry> + Send + Sync + 'static,
Sourcepub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>>
pub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>>
Install this collector 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
Event
s.
§Errors
Returns an Error if the initialization was unsuccessful, likely
because a global collector was already installed by another
call to try_init
.
Sourcepub fn init(self)
pub fn init(self)
Install this collector as the global default.
If the tracing-log
feature is enabled, this will also install
the LogTracer to convert Log
records into tracing
Event
s.
§Panics
Panics if the initialization was unsuccessful, likely because a
global collector was already installed by another call to try_init
.
Source§impl<N, L, T, F, W> CollectorBuilder<N, Format<L, T>, F, W>where
N: for<'writer> FormatFields<'writer> + 'static,
impl<N, L, T, F, W> CollectorBuilder<N, Format<L, T>, F, W>where
N: for<'writer> FormatFields<'writer> + 'static,
Sourcepub fn with_timer<T2>(
self,
timer: T2,
) -> CollectorBuilder<N, Format<L, T2>, F, W>
pub fn with_timer<T2>( self, timer: T2, ) -> CollectorBuilder<N, Format<L, T2>, F, W>
Use the given timer
for log message timestamps.
See the time
module for the provided timer implementations.
Note that using the "time
“” feature flag enables the
additional time formatters UtcTime
and LocalTime
, which use the
time
crate to provide more sophisticated timestamp formatting
options.
Sourcepub fn without_time(self) -> CollectorBuilder<N, Format<L, ()>, F, W>
pub fn without_time(self) -> CollectorBuilder<N, Format<L, ()>, F, W>
Do not emit timestamps with log messages.
Sourcepub fn with_span_events(self, kind: FmtSpan) -> Self
pub fn with_span_events(self, kind: FmtSpan) -> Self
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 Collector
s or by
Subscriber
s added to this subscriber.
Sourcepub fn with_ansi(self, ansi: bool) -> CollectorBuilder<N, Format<L, T>, F, W>
pub fn with_ansi(self, ansi: bool) -> CollectorBuilder<N, Format<L, T>, F, W>
Sets whether or not the formatter emits ANSI terminal escape codes for colors and other text formatting.
Enabling ANSI escapes (calling with_ansi(true)
) requires the “ansi”
crate feature flag. Calling with_ansi(true)
without the “ansi”
feature flag enabled will panic if debug assertions are enabled, or
print a warning otherwise.
This method itself is still available without the feature flag. This is to allow ANSI escape codes to be explicitly disabled without having to opt-in to the dependencies required to emit ANSI formatting. This way, code which constructs a formatter that should never emit ANSI escape codes can ensure that they are not used, regardless of whether or not other crates in the dependency graph enable the “ansi” feature flag.
Sourcepub fn log_internal_errors(
self,
log_internal_errors: bool,
) -> CollectorBuilder<N, Format<L, T>, F, W>
pub fn log_internal_errors( self, log_internal_errors: bool, ) -> CollectorBuilder<N, Format<L, T>, F, W>
Sets whether to write errors from FormatEvent
to the writer.
Defaults to true.
By default, fmt::Collector
will write any FormatEvent
-internal errors to
the writer. These errors are unlikely and will only occur if there is a
bug in the FormatEvent
implementation or its dependencies.
If writing to the writer fails, the error message is printed to stderr as a fallback.
Sourcepub fn with_target(
self,
display_target: bool,
) -> CollectorBuilder<N, Format<L, T>, F, W>
pub fn with_target( self, display_target: bool, ) -> CollectorBuilder<N, Format<L, T>, F, W>
Sets whether or not an event’s target is displayed.
Sourcepub fn with_file(
self,
display_filename: bool,
) -> CollectorBuilder<N, Format<L, T>, F, W>
pub fn with_file( self, display_filename: bool, ) -> CollectorBuilder<N, Format<L, T>, F, W>
Sets whether or not an event’s source code file path is displayed.
Sourcepub fn with_line_number(
self,
display_line_number: bool,
) -> CollectorBuilder<N, Format<L, T>, F, W>
pub fn with_line_number( self, display_line_number: bool, ) -> CollectorBuilder<N, Format<L, T>, F, W>
Sets whether or not an event’s source code line number is displayed.
Sourcepub fn with_level(
self,
display_level: bool,
) -> CollectorBuilder<N, Format<L, T>, F, W>
pub fn with_level( self, display_level: bool, ) -> CollectorBuilder<N, Format<L, T>, F, W>
Sets whether or not an event’s level is displayed.
Sourcepub fn with_thread_names(
self,
display_thread_names: bool,
) -> CollectorBuilder<N, Format<L, T>, F, W>
pub fn with_thread_names( self, display_thread_names: bool, ) -> CollectorBuilder<N, Format<L, T>, F, W>
Sets whether or not the name of the current thread is displayed when formatting events.
Sourcepub fn with_thread_ids(
self,
display_thread_ids: bool,
) -> CollectorBuilder<N, Format<L, T>, F, W>
pub fn with_thread_ids( self, display_thread_ids: bool, ) -> CollectorBuilder<N, Format<L, T>, F, W>
Sets whether or not the thread ID of the current thread is displayed when formatting events.
Sourcepub fn compact(self) -> CollectorBuilder<N, Format<Compact, T>, F, W>where
N: for<'writer> FormatFields<'writer> + 'static,
pub fn compact(self) -> CollectorBuilder<N, Format<Compact, T>, F, W>where
N: for<'writer> FormatFields<'writer> + 'static,
Sets the collector being built to use a less verbose formatter.
See format::Compact
for details.
Sourcepub fn pretty(self) -> CollectorBuilder<Pretty, Format<Pretty, T>, F, W>
Available on crate feature ansi
only.
pub fn pretty(self) -> CollectorBuilder<Pretty, Format<Pretty, T>, F, W>
ansi
only.Sets the collector being built to use an excessively pretty, human-readable formatter.
Sourcepub fn json(self) -> CollectorBuilder<JsonFields, Format<Json, T>, F, W>where
N: for<'writer> FormatFields<'writer> + 'static,
Available on crate feature json
only.
pub fn json(self) -> CollectorBuilder<JsonFields, Format<Json, T>, F, W>where
N: for<'writer> FormatFields<'writer> + 'static,
json
only.Sets the collector being built to use a JSON formatter.
See format::Json
for details.
Source§impl<T, F, W> CollectorBuilder<JsonFields, Format<Json, T>, F, W>
impl<T, F, W> CollectorBuilder<JsonFields, Format<Json, T>, F, W>
Sourcepub fn flatten_event(
self,
flatten_event: bool,
) -> CollectorBuilder<JsonFields, Format<Json, T>, F, W>
Available on crate feature json
only.
pub fn flatten_event( self, flatten_event: bool, ) -> CollectorBuilder<JsonFields, Format<Json, T>, F, W>
json
only.Sets the json collector being built to flatten event metadata.
See format::Json
for details.
Sourcepub fn with_current_span(
self,
display_current_span: bool,
) -> CollectorBuilder<JsonFields, Format<Json, T>, F, W>
Available on crate feature json
only.
pub fn with_current_span( self, display_current_span: bool, ) -> CollectorBuilder<JsonFields, Format<Json, T>, F, W>
json
only.Sets whether or not the JSON subscriber being built will include the current span in formatted events.
See format::Json
for details.
Sourcepub fn with_span_list(
self,
display_span_list: bool,
) -> CollectorBuilder<JsonFields, Format<Json, T>, F, W>
Available on crate feature json
only.
pub fn with_span_list( self, display_span_list: bool, ) -> CollectorBuilder<JsonFields, Format<Json, T>, F, W>
json
only.Sets whether or not the JSON subscriber being built will include a list (from root to leaf) of all currently entered spans in formatted events.
See format::Json
for details.
Source§impl<N, E, F, W> CollectorBuilder<N, E, Subscriber<F>, W>
impl<N, E, F, W> CollectorBuilder<N, E, Subscriber<F>, W>
Sourcepub fn reload_handle(&self) -> Handle<F>
pub fn reload_handle(&self) -> Handle<F>
Returns a Handle
that may be used to reload the constructed collector’s
filter.
Source§impl<N, E, F, W> CollectorBuilder<N, E, F, W>
impl<N, E, F, W> CollectorBuilder<N, E, F, W>
Sourcepub fn fmt_fields<N2>(self, fmt_fields: N2) -> CollectorBuilder<N2, E, F, W>where
N2: for<'writer> FormatFields<'writer> + 'static,
pub fn fmt_fields<N2>(self, fmt_fields: N2) -> CollectorBuilder<N2, E, F, W>where
N2: for<'writer> FormatFields<'writer> + 'static,
Sets the Visitor that the collector being built will use to record fields.
For example:
use tracing_subscriber::fmt::format;
use tracing_subscriber::field::MakeExt;
let formatter =
// Construct a custom formatter for `Debug` fields
format::debug_fn(|writer, field, value| write!(writer, "{}: {:?}", field, value))
// Use the `tracing_subscriber::MakeExt` trait to wrap the
// formatter so that a delimiter is added between fields.
.delimited(", ");
let collector = tracing_subscriber::fmt()
.fmt_fields(formatter)
.finish();
Sourcepub fn with_env_filter(
self,
filter: impl Into<EnvFilter>,
) -> CollectorBuilder<N, E, EnvFilter, W>
Available on crate feature env-filter
only.
pub fn with_env_filter( self, filter: impl Into<EnvFilter>, ) -> CollectorBuilder<N, E, EnvFilter, W>
env-filter
only.Sets the EnvFilter
that the collector 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::{fmt, EnvFilter};
fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
Setting a filter based on a pre-set filter directive string:
use tracing_subscriber::fmt;
fmt()
.with_env_filter("my_crate=info,my_crate::my_mod=debug,[my_span]=trace")
.init();
Adding additional directives to a filter constructed from an env var:
use tracing_subscriber::{fmt, 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()?);
fmt()
.with_env_filter(filter)
.try_init()?;
Sourcepub fn with_max_level(
self,
filter: impl Into<LevelFilter>,
) -> CollectorBuilder<N, E, LevelFilter, W>
pub fn with_max_level( self, filter: impl Into<LevelFilter>, ) -> CollectorBuilder<N, E, LevelFilter, W>
Sets the maximum verbosity level that will be enabled by the collector.
If the max level has already been set, or a EnvFilter
was added by
with_env_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 collector won’t record any spans or events!
use tracing_subscriber::{fmt, filter::LevelFilter};
let subscriber = fmt()
.with_max_level(LevelFilter::OFF)
.finish();
Sourcepub fn with_filter_reloading(self) -> CollectorBuilder<N, E, Subscriber<F>, W>
pub fn with_filter_reloading(self) -> CollectorBuilder<N, E, Subscriber<F>, W>
Configures the collector being built to allow filter reloading at runtime.
The returned builder will have a reload_handle
method, which returns
a reload::Handle
that may be used to set a new filter value.
For example:
use tracing::Level;
use tracing_subscriber::util::SubscriberInitExt;
let builder = tracing_subscriber::fmt()
// Set a max level filter on the collector
.with_max_level(Level::INFO)
.with_filter_reloading();
// Get a handle for modifying the collector's max level filter.
let handle = builder.reload_handle();
// Finish building the collector, and set it as the default.
builder.finish().init();
// Currently, the max level is INFO, so this event will be disabled.
tracing::debug!("this is not recorded!");
// Use the handle to set a new max level filter.
// (this returns an error if the collector has been dropped, which shouldn't
// happen in this example.)
handle.reload(Level::DEBUG).expect("the collector should still exist");
// Now, the max level is INFO, so this event will be recorded.
tracing::debug!("this is recorded!");
Sourcepub fn event_format<E2>(self, fmt_event: E2) -> CollectorBuilder<N, E2, F, W>where
E2: FormatEvent<Registry, N> + 'static,
N: for<'writer> FormatFields<'writer> + 'static,
W: for<'writer> MakeWriter<'writer> + 'static,
pub fn event_format<E2>(self, fmt_event: E2) -> CollectorBuilder<N, E2, F, W>where
E2: FormatEvent<Registry, N> + 'static,
N: for<'writer> FormatFields<'writer> + 'static,
W: for<'writer> MakeWriter<'writer> + 'static,
Sets the event formatter that the subscriber being built will use to format events that occur.
The event formatter may be any type implementing the FormatEvent
trait, which is implemented for all functions taking a FmtContext
, a
Writer
, and an Event
.
§Examples
Setting a type implementing FormatEvent
as the formatter:
use tracing_subscriber::fmt::format;
let subscriber = tracing_subscriber::fmt()
.event_format(format().compact())
.finish();
Sourcepub fn with_writer<W2>(self, make_writer: W2) -> CollectorBuilder<N, E, F, W2>where
W2: for<'writer> MakeWriter<'writer> + 'static,
pub fn with_writer<W2>(self, make_writer: W2) -> CollectorBuilder<N, E, F, W2>where
W2: for<'writer> MakeWriter<'writer> + 'static,
Sets the MakeWriter
that the collector 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();
Sourcepub fn with_test_writer(self) -> CollectorBuilder<N, E, F, TestWriter>
pub fn with_test_writer(self) -> CollectorBuilder<N, E, F, TestWriter>
Configures the collector 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::collect;
collect::set_default(
fmt()
.with_test_writer()
.finish()
);
Sourcepub fn map_event_format<E2>(
self,
f: impl FnOnce(E) -> E2,
) -> CollectorBuilder<N, E2, F, W>where
E2: FormatEvent<Registry, N> + 'static,
N: for<'writer> FormatFields<'writer> + 'static,
W: for<'writer> MakeWriter<'writer> + 'static,
pub fn map_event_format<E2>(
self,
f: impl FnOnce(E) -> E2,
) -> CollectorBuilder<N, E2, F, W>where
E2: FormatEvent<Registry, N> + 'static,
N: for<'writer> FormatFields<'writer> + 'static,
W: for<'writer> MakeWriter<'writer> + 'static,
Updates the event formatter by applying a function to the existing event formatter.
This sets the event formatter that the collector being built will use to record fields.
§Examples
Updating an event formatter:
let subscriber = tracing_subscriber::fmt()
.map_event_format(|e| e.compact())
.finish();
Sourcepub fn map_fmt_fields<N2>(
self,
f: impl FnOnce(N) -> N2,
) -> CollectorBuilder<N2, E, F, W>where
N2: for<'writer> FormatFields<'writer> + 'static,
pub fn map_fmt_fields<N2>(
self,
f: impl FnOnce(N) -> N2,
) -> CollectorBuilder<N2, E, F, W>where
N2: for<'writer> FormatFields<'writer> + 'static,
Updates the field formatter by applying a function to the existing field formatter.
This sets the field formatter that the subscriber being built will use to record fields.
§Examples
Updating a field formatter:
use tracing_subscriber::field::MakeExt;
let subscriber = tracing_subscriber::fmt()
.map_fmt_fields(|f| f.debug_alt())
.finish();
Sourcepub fn map_writer<W2>(
self,
f: impl FnOnce(W) -> W2,
) -> CollectorBuilder<N, E, F, W2>where
W2: for<'writer> MakeWriter<'writer> + 'static,
pub fn map_writer<W2>(
self,
f: impl FnOnce(W) -> W2,
) -> CollectorBuilder<N, E, F, W2>where
W2: for<'writer> MakeWriter<'writer> + 'static,
Updates the MakeWriter
by applying a function to the existing MakeWriter
.
This sets the MakeWriter
that the subscriber being built will use to write events.
§Examples
Redirect output to stderr if level is <= WARN:
use tracing::Level;
use tracing_subscriber::fmt::{self, writer::MakeWriterExt};
let stderr = std::io::stderr.with_max_level(Level::WARN);
let collector = tracing_subscriber::fmt()
.map_writer(move |w| stderr.or_else(w))
.finish();
Trait Implementations§
Source§impl Default for CollectorBuilder
impl Default for CollectorBuilder
Source§impl<N, E, F, W> From<CollectorBuilder<N, E, F, W>> for Dispatchwhere
N: for<'writer> FormatFields<'writer> + 'static,
E: FormatEvent<Registry, N> + 'static,
W: for<'writer> MakeWriter<'writer> + 'static,
F: Subscribe<Formatter<N, E, W>> + Send + Sync + 'static,
Subscriber<Registry, N, E, W>: Subscribe<Registry> + Send + Sync + 'static,
impl<N, E, F, W> From<CollectorBuilder<N, E, F, W>> for Dispatchwhere
N: for<'writer> FormatFields<'writer> + 'static,
E: FormatEvent<Registry, N> + 'static,
W: for<'writer> MakeWriter<'writer> + 'static,
F: Subscribe<Formatter<N, E, W>> + Send + Sync + 'static,
Subscriber<Registry, N, E, W>: Subscribe<Registry> + Send + Sync + 'static,
Source§fn from(builder: CollectorBuilder<N, E, F, W>) -> Dispatch
fn from(builder: CollectorBuilder<N, E, F, W>) -> Dispatch
Auto Trait Implementations§
impl<N, E, F, W> Freeze for CollectorBuilder<N, E, F, W>
impl<N, E, F, W> RefUnwindSafe for CollectorBuilder<N, E, F, W>
impl<N, E, F, W> Send for CollectorBuilder<N, E, F, W>
impl<N, E, F, W> Sync for CollectorBuilder<N, E, F, W>
impl<N, E, F, W> Unpin for CollectorBuilder<N, E, F, W>
impl<N, E, F, W> UnwindSafe for CollectorBuilder<N, E, F, W>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> SubscriberInitExt for T
impl<T> SubscriberInitExt for T
Source§fn set_default(self) -> DefaultGuard
fn set_default(self) -> DefaultGuard
std
only.self
as the default subscriber in the current scope, returning a
guard that will unset it when dropped. Read moreSource§fn try_init(self) -> Result<(), TryInitError>
fn try_init(self) -> Result<(), TryInitError>
self
as the global default subscriber in the current
scope, returning an error if one is already set. Read moreSource§fn init(self)
fn init(self)
self
as the global default subscriber in the current
scope, panicking if this fails. Read more