1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
use crate::{
filter::{
directive::{DirectiveParseError, DirectiveSet, StaticDirective},
LevelFilter,
},
layer,
};
use std::{
iter::{Extend, FromIterator},
str::FromStr,
};
use tracing_core::{Interest, Metadata, Subscriber};
/// 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][plf] (using its
/// [`Filter`] implementation) and [global filtering][global] (using its
/// [`Layer`] implementation).
///
/// See the [documentation on filtering with layers][filtering] 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);
/// # drop(filter);
/// ```
///
/// Alternatively, `Targets` implements [`std::str::FromStr`], allowing it to be
/// parsed from a comma-delimited list of `target=level` pairs. For example:
///
/// ```rust
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// 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)
/// );
/// # Ok(()) }
/// ```
///
/// 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][plf] *and* as a
/// [global filter]:
///
/// ```rust
/// use tracing_subscriber::{
/// fmt,
/// filter::{Targets, LevelFilter},
/// prelude::*,
/// };
/// use tracing_core::Level;
/// use std::{sync::Arc, fs::File};
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
///
/// // 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();
/// # Ok(()) }
///```
///
/// [target]: tracing_core::Metadata::target
/// [level]: tracing_core::Level
/// [`Filter`]: crate::layer::Filter
/// [`Layer`]: crate::layer::Layer
/// [plf]: crate::layer#per-layer-filtering
/// [global]: crate::layer#global-filtering
/// [filtering]: crate::layer#filtering-with-layers
/// [`env_logger` crate]: https://docs.rs/env_logger/0.9.0/env_logger/index.html#enabling-logging
/// [`EnvFilter`]: crate::filter::EnvFilter
#[derive(Debug, Default, Clone, PartialEq)]
pub struct Targets(DirectiveSet<StaticDirective>);
impl Targets {
/// 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.
///
/// [`with_target`]: Targets::with_target
/// [`with_targets`]: Targets::with_targets
/// [`with_default`]: Targets::with_default
pub fn new() -> Self {
Self::default()
}
/// Enables spans and events with [target]s 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);
/// # drop(filter);
/// ```
///
/// [`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);
/// # drop(filter);
/// ```
///
/// [target]: tracing_core::Metadata::target
pub fn with_target(mut self, target: impl Into<String>, level: impl Into<LevelFilter>) -> Self {
self.0.add(StaticDirective::new(
Some(target.into()),
Default::default(),
level.into(),
));
self
}
/// Adds [target]s 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)
/// ]);
/// # drop(filter);
/// ```
///
/// [`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);
/// # drop(filter);
/// ```
///
/// [target]: tracing_core::Metadata::target
pub fn with_targets<T, L>(mut self, targets: impl IntoIterator<Item = (T, L)>) -> Self
where
String: From<T>,
LevelFilter: From<L>,
{
self.extend(targets);
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.
pub fn with_default(mut self, level: impl Into<LevelFilter>) -> Self {
self.0
.add(StaticDirective::new(None, Default::default(), level.into()));
self
}
#[inline]
fn interested(&self, metadata: &'static Metadata<'static>) -> Interest {
if self.0.enabled(metadata) {
Interest::always()
} else {
Interest::never()
}
}
}
impl<T, L> Extend<(T, L)> for Targets
where
T: Into<String>,
L: Into<LevelFilter>,
{
fn extend<I: IntoIterator<Item = (T, L)>>(&mut self, iter: I) {
let iter = iter.into_iter().map(|(target, level)| {
StaticDirective::new(Some(target.into()), Default::default(), level.into())
});
self.0.extend(iter);
}
}
impl<T, L> FromIterator<(T, L)> for Targets
where
T: Into<String>,
L: Into<LevelFilter>,
{
fn from_iter<I: IntoIterator<Item = (T, L)>>(iter: I) -> Self {
let mut this = Self::default();
this.extend(iter);
this
}
}
impl FromStr for Targets {
type Err = DirectiveParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.split(',')
.map(StaticDirective::from_str)
.collect::<Result<_, _>>()
.map(Self)
}
}
impl<S> layer::Layer<S> for Targets
where
S: Subscriber,
{
fn enabled(&self, metadata: &Metadata<'_>, _: layer::Context<'_, S>) -> bool {
self.0.enabled(metadata)
}
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
self.interested(metadata)
}
fn max_level_hint(&self) -> Option<LevelFilter> {
Some(self.0.max_level)
}
}
#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
impl<S> layer::Filter<S> for Targets {
fn enabled(&self, metadata: &Metadata<'_>, _: &layer::Context<'_, S>) -> bool {
self.0.enabled(metadata)
}
fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest {
self.interested(metadata)
}
fn max_level_hint(&self) -> Option<LevelFilter> {
Some(self.0.max_level)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::filter::directive::FilterVec;
fn expect_parse(s: &str) -> FilterVec<StaticDirective> {
match dbg!(s).parse::<Targets>() {
Err(e) => panic!("string {:?} did not parse successfully: {}", s, e),
Ok(e) => e.0.into_vec(),
}
}
fn expect_parse_ralith(s: &str) {
let dirs = expect_parse(s);
assert_eq!(dirs.len(), 2, "\nparsed: {:#?}", dirs);
assert_eq!(dirs[0].target, Some("server".to_string()));
assert_eq!(dirs[0].level, LevelFilter::DEBUG);
assert_eq!(dirs[0].field_names, FilterVec::<String>::default());
assert_eq!(dirs[1].target, Some("common".to_string()));
assert_eq!(dirs[1].level, LevelFilter::INFO);
assert_eq!(dirs[1].field_names, FilterVec::<String>::default());
}
fn expect_parse_level_directives(s: &str) {
let dirs = expect_parse(s);
assert_eq!(dirs.len(), 6, "\nparsed: {:#?}", dirs);
assert_eq!(dirs[0].target, Some("crate3::mod2::mod1".to_string()));
assert_eq!(dirs[0].level, LevelFilter::OFF);
assert_eq!(dirs[0].field_names, FilterVec::<String>::default());
assert_eq!(dirs[1].target, Some("crate1::mod2::mod3".to_string()));
assert_eq!(dirs[1].level, LevelFilter::INFO);
assert_eq!(dirs[1].field_names, FilterVec::<String>::default());
assert_eq!(dirs[2].target, Some("crate1::mod2".to_string()));
assert_eq!(dirs[2].level, LevelFilter::WARN);
assert_eq!(dirs[2].field_names, FilterVec::<String>::default());
assert_eq!(dirs[3].target, Some("crate1::mod1".to_string()));
assert_eq!(dirs[3].level, LevelFilter::ERROR);
assert_eq!(dirs[3].field_names, FilterVec::<String>::default());
assert_eq!(dirs[4].target, Some("crate3".to_string()));
assert_eq!(dirs[4].level, LevelFilter::TRACE);
assert_eq!(dirs[4].field_names, FilterVec::<String>::default());
assert_eq!(dirs[5].target, Some("crate2".to_string()));
assert_eq!(dirs[5].level, LevelFilter::DEBUG);
assert_eq!(dirs[5].field_names, FilterVec::<String>::default());
}
#[test]
fn parse_ralith() {
expect_parse_ralith("common=info,server=debug");
}
#[test]
fn parse_ralith_uc() {
expect_parse_ralith("common=INFO,server=DEBUG");
}
#[test]
fn parse_ralith_mixed() {
expect_parse("common=iNfo,server=dEbUg");
}
#[test]
fn expect_parse_valid() {
let dirs = expect_parse("crate1::mod1=error,crate1::mod2,crate2=debug,crate3=off");
assert_eq!(dirs.len(), 4, "\nparsed: {:#?}", dirs);
assert_eq!(dirs[0].target, Some("crate1::mod2".to_string()));
assert_eq!(dirs[0].level, LevelFilter::TRACE);
assert_eq!(dirs[0].field_names, FilterVec::<String>::default());
assert_eq!(dirs[1].target, Some("crate1::mod1".to_string()));
assert_eq!(dirs[1].level, LevelFilter::ERROR);
assert_eq!(dirs[1].field_names, FilterVec::<String>::default());
assert_eq!(dirs[2].target, Some("crate3".to_string()));
assert_eq!(dirs[2].level, LevelFilter::OFF);
assert_eq!(dirs[2].field_names, FilterVec::<String>::default());
assert_eq!(dirs[3].target, Some("crate2".to_string()));
assert_eq!(dirs[3].level, LevelFilter::DEBUG);
assert_eq!(dirs[3].field_names, FilterVec::<String>::default());
}
#[test]
fn parse_level_directives() {
expect_parse_level_directives(
"crate1::mod1=error,crate1::mod2=warn,crate1::mod2::mod3=info,\
crate2=debug,crate3=trace,crate3::mod2::mod1=off",
)
}
#[test]
fn parse_uppercase_level_directives() {
expect_parse_level_directives(
"crate1::mod1=ERROR,crate1::mod2=WARN,crate1::mod2::mod3=INFO,\
crate2=DEBUG,crate3=TRACE,crate3::mod2::mod1=OFF",
)
}
#[test]
fn parse_numeric_level_directives() {
expect_parse_level_directives(
"crate1::mod1=1,crate1::mod2=2,crate1::mod2::mod3=3,crate2=4,\
crate3=5,crate3::mod2::mod1=0",
)
}
}