pub struct Targets(/* private fields */);
std
or alloc
only.Expand description
A filter that enables or disables spans and events based on their target and level.
Targets are typically equal to the Rust module path of the code where the span or event was recorded, although they may be overridden.
This type can be used for both per-layer filtering (using its
Filter
implementation) and global filtering (using its
Layer
implementation).
See the documentation on filtering with layers for details.
§Filtering With Targets
A Targets
filter consists of one or more target prefixes, paired with
LevelFilter
s. If a span or event’s target begins with one of those
prefixes, and its level is at or below the LevelFilter
enabled for
that prefix, then the span or event will be enabled.
This is similar to the behavior implemented by the env_logger
crate in
the log
ecosystem.
The EnvFilter
type also provided by this crate is very similar to Targets
,
but is capable of a more sophisticated form of filtering where events may
also be enabled or disabled based on the span they are recorded in.
Targets
can be thought of as a lighter-weight form of EnvFilter
that
can be used instead when this dynamic filtering is not required.
§Examples
A Targets
filter can be constructed by programmatically adding targets and
levels to enable:
use tracing_subscriber::{filter, prelude::*};
use tracing_core::Level;
let filter = filter::Targets::new()
// Enable the `INFO` level for anything in `my_crate`
.with_target("my_crate", Level::INFO)
// Enable the `DEBUG` level for a specific module.
.with_target("my_crate::interesting_module", Level::DEBUG);
// Build a new subscriber with the `fmt` layer using the `Targets`
// filter we constructed above.
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(filter)
.init();
LevelFilter::OFF
can be used to disable a particular target:
use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;
let filter = Targets::new()
.with_target("my_crate", Level::INFO)
// Disable all traces from `annoying_module`.
.with_target("my_crate::annoying_module", LevelFilter::OFF);
Alternatively, Targets
implements std::str::FromStr
, allowing it to be
parsed from a comma-delimited list of target=level
pairs. For example:
use tracing_subscriber::filter;
use tracing_core::Level;
let filter = "my_crate=info,my_crate::interesting_module=trace,other_crate=debug"
.parse::<filter::Targets>()?;
// The parsed filter is identical to a filter constructed using `with_target`:
assert_eq!(
filter,
filter::Targets::new()
.with_target("my_crate", Level::INFO)
.with_target("my_crate::interesting_module", Level::TRACE)
.with_target("other_crate", Level::DEBUG)
);
This is particularly useful when the list of enabled targets is configurable by the user at runtime.
The Targets
filter can be used as a per-layer filter and as a
global filter:
use tracing_subscriber::{
fmt,
filter::{Targets, LevelFilter},
prelude::*,
};
use tracing_core::Level;
use std::{sync::Arc, fs::File};
// A layer that logs events to stdout using the human-readable "pretty"
// format.
let stdout_log = fmt::layer().pretty();
// A layer that logs events to a file, using the JSON format.
let file = File::create("debug_log.json")?;
let debug_log = fmt::layer()
.with_writer(Arc::new(file))
.json();
tracing_subscriber::registry()
// Only log INFO and above to stdout, unless the span or event
// has the `my_crate::cool_module` target prefix.
.with(stdout_log
.with_filter(
Targets::default()
.with_target("my_crate::cool_module", Level::DEBUG)
.with_default(Level::INFO)
)
)
// Log everything enabled by the global filter to `debug_log.json`.
.with(debug_log)
// Configure a global filter for the whole subscriber stack. This will
// control what spans and events are recorded by both the `debug_log`
// and the `stdout_log` layers, and `stdout_log` will *additionally* be
// filtered by its per-layer filter.
.with(
Targets::default()
.with_target("my_crate", Level::TRACE)
.with_target("other_crate", Level::INFO)
.with_target("other_crate::annoying_module", LevelFilter::OFF)
.with_target("third_crate", Level::DEBUG)
).init();
Implementations§
Source§impl Targets
impl Targets
Sourcepub fn new() -> Self
pub fn new() -> Self
Returns a new Targets
filter.
This filter will enable no targets. Call with_target
or with_targets
to add enabled targets, and with_default
to change the default level
enabled for spans and events that didn’t match any of the provided targets.
Sourcepub fn with_target(
self,
target: impl Into<String>,
level: impl Into<LevelFilter>,
) -> Self
pub fn with_target( self, target: impl Into<String>, level: impl Into<LevelFilter>, ) -> Self
Enables spans and events with targets starting with the provided target
prefix if they are at or below the provided LevelFilter
.
§Examples
use tracing_subscriber::filter;
use tracing_core::Level;
let filter = filter::Targets::new()
// Enable the `INFO` level for anything in `my_crate`
.with_target("my_crate", Level::INFO)
// Enable the `DEBUG` level for a specific module.
.with_target("my_crate::interesting_module", Level::DEBUG);
LevelFilter::OFF
can be used to disable a particular target:
use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;
let filter = Targets::new()
.with_target("my_crate", Level::INFO)
// Disable all traces from `annoying_module`.
.with_target("my_crate::interesting_module", LevelFilter::OFF);
Sourcepub fn with_targets<T, L>(
self,
targets: impl IntoIterator<Item = (T, L)>,
) -> Self
pub fn with_targets<T, L>( self, targets: impl IntoIterator<Item = (T, L)>, ) -> Self
Adds targets from an iterator of target-LevelFilter
pairs to this filter.
§Examples
use tracing_subscriber::filter;
use tracing_core::Level;
let filter = filter::Targets::new()
.with_targets(vec![
("my_crate", Level::INFO),
("my_crate::some_module", Level::DEBUG),
("my_crate::other_module::cool_stuff", Level::TRACE),
("other_crate", Level::WARN)
]);
LevelFilter::OFF
can be used to disable a particular target:
use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;
let filter = Targets::new()
.with_target("my_crate", Level::INFO)
// Disable all traces from `annoying_module`.
.with_target("my_crate::interesting_module", LevelFilter::OFF);
Sourcepub fn with_default(self, level: impl Into<LevelFilter>) -> Self
pub fn with_default(self, level: impl Into<LevelFilter>) -> Self
Sets the default level to enable for spans and events whose targets did not match any of the configured prefixes.
By default, this is LevelFilter::OFF
. This means that spans and
events will only be enabled if they match one of the configured target
prefixes. If this is changed to a different LevelFilter
, spans and
events with targets that did not match any of the configured prefixes
will be enabled if their level is at or below the provided level.
Sourcepub fn default_level(&self) -> Option<LevelFilter> ⓘ
pub fn default_level(&self) -> Option<LevelFilter> ⓘ
Returns the default level for this filter, if one is set.
The default level is used to filter any spans or events with targets that do not match any of the configured set of prefixes.
The default level can be set for a filter either by using
with_default
or when parsing from a filter string that includes a
level without a target (e.g. "trace"
).
§Examples
use tracing_subscriber::filter::{LevelFilter, Targets};
let filter = Targets::new().with_default(LevelFilter::INFO);
assert_eq!(filter.default_level(), Some(LevelFilter::INFO));
let filter: Targets = "info".parse().unwrap();
assert_eq!(filter.default_level(), Some(LevelFilter::INFO));
The default level is None
if no default is set:
use tracing_subscriber::filter::Targets;
let filter = Targets::new();
assert_eq!(filter.default_level(), None);
let filter: Targets = "my_crate=info".parse().unwrap();
assert_eq!(filter.default_level(), None);
Note that an unset default level (None
) behaves like LevelFilter::OFF
when the filter is
used, but it could also be set explicitly which may be useful to distinguish (such as when
merging multiple Targets
).
use tracing_subscriber::filter::{LevelFilter, Targets};
let filter = Targets::new().with_default(LevelFilter::OFF);
assert_eq!(filter.default_level(), Some(LevelFilter::OFF));
let filter: Targets = "off".parse().unwrap();
assert_eq!(filter.default_level(), Some(LevelFilter::OFF));
Sourcepub fn iter(&self) -> Iter<'_> ⓘ
pub fn iter(&self) -> Iter<'_> ⓘ
Returns an iterator over the target-LevelFilter
pairs in this filter.
The order of iteration is undefined.
§Examples
use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;
let filter = Targets::new()
.with_target("my_crate", Level::INFO)
.with_target("my_crate::interesting_module", Level::DEBUG);
let mut targets: Vec<_> = filter.iter().collect();
targets.sort();
assert_eq!(targets, vec![
("my_crate", LevelFilter::INFO),
("my_crate::interesting_module", LevelFilter::DEBUG),
]);
Sourcepub fn would_enable(&self, target: &str, level: &Level) -> bool
pub fn would_enable(&self, target: &str, level: &Level) -> bool
Returns whether a target-Level
pair would be enabled
by this Targets
.
This method can be used with module_path!
from std
as the target
in order to emulate the behavior of the tracing::event!
and tracing::span!
macros.
§Examples
use tracing_subscriber::filter::{Targets, LevelFilter};
use tracing_core::Level;
let filter = Targets::new()
.with_target("my_crate", Level::INFO)
.with_target("my_crate::interesting_module", Level::DEBUG);
assert!(filter.would_enable("my_crate", &Level::INFO));
assert!(!filter.would_enable("my_crate::interesting_module", &Level::TRACE));
Trait Implementations§
Source§impl<T, L> Extend<(T, L)> for Targets
impl<T, L> Extend<(T, L)> for Targets
Source§fn extend<I: IntoIterator<Item = (T, L)>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = (T, L)>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
#72631)Source§impl<S> Filter<S> for Targets
Available on crate feature registry
only.
impl<S> Filter<S> for Targets
registry
only.Source§fn enabled(&self, metadata: &Metadata<'_>, _: &Context<'_, S>) -> bool
fn enabled(&self, metadata: &Metadata<'_>, _: &Context<'_, S>) -> bool
std
only.true
if this layer is interested in a span or event with the
given Metadata
in the current Context
, similarly to
Subscriber::enabled
. Read moreSource§fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest
fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest
std
only.Source§fn max_level_hint(&self) -> Option<LevelFilter> ⓘ
fn max_level_hint(&self) -> Option<LevelFilter> ⓘ
std
only.Source§fn event_enabled(&self, event: &Event<'_>, cx: &Context<'_, S>) -> bool
fn event_enabled(&self, event: &Event<'_>, cx: &Context<'_, S>) -> bool
std
only.Layer]'s [
on_event], to determine if
on_event` should be called. Read moreSource§fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
std
only.Source§fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>)
fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>)
std
only.Source§fn on_enter(&self, id: &Id, ctx: Context<'_, S>)
fn on_enter(&self, id: &Id, ctx: Context<'_, S>)
std
only.Source§impl<T, L> FromIterator<(T, L)> for Targets
impl<T, L> FromIterator<(T, L)> for Targets
Source§impl<'a> IntoIterator for &'a Targets
impl<'a> IntoIterator for &'a Targets
Source§impl IntoIterator for Targets
impl IntoIterator for Targets
Source§impl<S> Layer<S> for Targetswhere
S: Subscriber,
impl<S> Layer<S> for Targetswhere
S: Subscriber,
Source§fn enabled(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool
fn enabled(&self, metadata: &Metadata<'_>, _: Context<'_, S>) -> bool
true
if this layer is interested in a span or event with the
given metadata
in the current Context
, similarly to
Subscriber::enabled
. Read moreSource§fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
Subscriber::register_callsite
. Read moreSource§fn on_register_dispatch(&self, subscriber: &Dispatch)
fn on_register_dispatch(&self, subscriber: &Dispatch)
Subscriber
. Read moreSource§fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
Attributes
and Id
.Source§fn on_record(&self, _span: &Id, _values: &Record<'_>, _ctx: Context<'_, S>)
fn on_record(&self, _span: &Id, _values: &Record<'_>, _ctx: Context<'_, S>)
Id
recorded the given
values
.Source§fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)
fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)
span
recorded that it
follows from the span with the ID follows
.Source§fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, S>)
fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, S>)
Source§fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)
fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)
Source§fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)
fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)
Source§fn on_close(&self, _id: Id, _ctx: Context<'_, S>)
fn on_close(&self, _id: Id, _ctx: Context<'_, S>)
Source§fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)
fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)
Source§fn and_then<L>(self, layer: L) -> Layered<L, Self, S> ⓘ
fn and_then<L>(self, layer: L) -> Layered<L, Self, S> ⓘ
Layer
, returning a Layered
struct implementing Layer
. Read moreSource§fn with_subscriber(self, inner: S) -> Layered<Self, S> ⓘwhere
Self: Sized,
fn with_subscriber(self, inner: S) -> Layered<Self, S> ⓘwhere
Self: Sized,
Layer
with the given Subscriber
, returning a
Layered
struct that implements Subscriber
. Read moreSource§fn with_filter<F>(self, filter: F) -> Filtered<Self, F, S> ⓘ
fn with_filter<F>(self, filter: F) -> Filtered<Self, F, S> ⓘ
registry
and std
only.impl StructuralPartialEq for Targets
Auto Trait Implementations§
impl Freeze for Targets
impl RefUnwindSafe for Targets
impl Send for Targets
impl Sync for Targets
impl Unpin for Targets
impl UnwindSafe for Targets
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<F, S> FilterExt<S> for Fwhere
F: Filter<S>,
impl<F, S> FilterExt<S> for Fwhere
F: Filter<S>,
Source§fn and<B>(self, other: B) -> And<Self, B, S> ⓘ
fn and<B>(self, other: B) -> And<Self, B, S> ⓘ
registry
and std
only.Source§fn or<B>(self, other: B) -> Or<Self, B, S> ⓘ
fn or<B>(self, other: B) -> Or<Self, B, S> ⓘ
registry
and std
only.