1 | use super::*; |
2 | use tracing_mock::event; |
3 | use tracing_subscriber::{ |
4 | filter::{filter_fn, Targets}, |
5 | prelude::*, |
6 | }; |
7 | |
8 | #[test] |
9 | #[cfg_attr (not(feature = "tracing-log" ), ignore)] |
10 | fn log_events() { |
11 | // Reproduces https://github.com/tokio-rs/tracing/issues/1563 |
12 | mod inner { |
13 | pub(super) const MODULE_PATH: &str = module_path!(); |
14 | |
15 | #[tracing::instrument ] |
16 | pub(super) fn logs() { |
17 | log::debug!("inner" ); |
18 | } |
19 | } |
20 | |
21 | let filter = Targets::new() |
22 | .with_default(LevelFilter::DEBUG) |
23 | .with_target(inner::MODULE_PATH, LevelFilter::WARN); |
24 | |
25 | let layer = |
26 | tracing_subscriber::layer::Identity::new().with_filter(filter_fn(move |_meta| true)); |
27 | |
28 | let _guard = tracing_subscriber::registry() |
29 | .with(filter) |
30 | .with(layer) |
31 | .set_default(); |
32 | |
33 | inner::logs(); |
34 | } |
35 | |
36 | #[test] |
37 | fn inner_layer_short_circuits() { |
38 | // This test ensures that when a global filter short-circuits `Interest` |
39 | // evaluation, we aren't left with a "dirty" per-layer filter state. |
40 | |
41 | let (layer, handle) = layer::mock() |
42 | .event(event::msg("hello world" )) |
43 | .only() |
44 | .run_with_handle(); |
45 | |
46 | let filter = Targets::new().with_target("magic_target" , LevelFilter::DEBUG); |
47 | |
48 | let _guard = tracing_subscriber::registry() |
49 | // Note: we don't just use a `LevelFilter` for the global filter here, |
50 | // because it will just return a max level filter, and the chain of |
51 | // `register_callsite` calls that would trigger the bug never happens... |
52 | .with(filter::filter_fn(|meta| meta.level() <= &Level::INFO)) |
53 | .with(layer.with_filter(filter)) |
54 | .set_default(); |
55 | |
56 | tracing::debug!("skip me please!" ); |
57 | tracing::info!(target: "magic_target" , "hello world" ); |
58 | |
59 | handle.assert_finished(); |
60 | } |
61 | |