1 | use super::*; |
2 | use tracing::Subscriber; |
3 | use tracing_mock::{expect, layer::MockLayer}; |
4 | |
5 | #[test] |
6 | fn with_filters_unboxed() { |
7 | let (trace_layer, trace_handle) = layer::named("trace" ) |
8 | .event(expect::event().at_level(Level::TRACE)) |
9 | .event(expect::event().at_level(Level::DEBUG)) |
10 | .event(expect::event().at_level(Level::INFO)) |
11 | .only() |
12 | .run_with_handle(); |
13 | let trace_layer = trace_layer.with_filter(LevelFilter::TRACE); |
14 | |
15 | let (debug_layer, debug_handle) = layer::named("debug" ) |
16 | .event(expect::event().at_level(Level::DEBUG)) |
17 | .event(expect::event().at_level(Level::INFO)) |
18 | .only() |
19 | .run_with_handle(); |
20 | let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG); |
21 | |
22 | let (info_layer, info_handle) = layer::named("info" ) |
23 | .event(expect::event().at_level(Level::INFO)) |
24 | .only() |
25 | .run_with_handle(); |
26 | let info_layer = info_layer.with_filter(LevelFilter::INFO); |
27 | |
28 | let _subscriber = tracing_subscriber::registry() |
29 | .with(vec![trace_layer, debug_layer, info_layer]) |
30 | .set_default(); |
31 | |
32 | tracing::trace!("hello trace" ); |
33 | tracing::debug!("hello debug" ); |
34 | tracing::info!("hello info" ); |
35 | |
36 | trace_handle.assert_finished(); |
37 | debug_handle.assert_finished(); |
38 | info_handle.assert_finished(); |
39 | } |
40 | |
41 | #[test] |
42 | fn with_filters_boxed() { |
43 | let (unfiltered_layer, unfiltered_handle) = layer::named("unfiltered" ) |
44 | .event(expect::event().at_level(Level::TRACE)) |
45 | .event(expect::event().at_level(Level::DEBUG)) |
46 | .event(expect::event().at_level(Level::INFO)) |
47 | .only() |
48 | .run_with_handle(); |
49 | let unfiltered_layer = unfiltered_layer.boxed(); |
50 | |
51 | let (debug_layer, debug_handle) = layer::named("debug" ) |
52 | .event(expect::event().at_level(Level::DEBUG)) |
53 | .event(expect::event().at_level(Level::INFO)) |
54 | .only() |
55 | .run_with_handle(); |
56 | let debug_layer = debug_layer.with_filter(LevelFilter::DEBUG).boxed(); |
57 | |
58 | let (target_layer, target_handle) = layer::named("target" ) |
59 | .event(expect::event().at_level(Level::INFO)) |
60 | .only() |
61 | .run_with_handle(); |
62 | let target_layer = target_layer |
63 | .with_filter(filter::filter_fn(|meta| meta.target() == "my_target" )) |
64 | .boxed(); |
65 | |
66 | let _subscriber = tracing_subscriber::registry() |
67 | .with(vec![unfiltered_layer, debug_layer, target_layer]) |
68 | .set_default(); |
69 | |
70 | tracing::trace!("hello trace" ); |
71 | tracing::debug!("hello debug" ); |
72 | tracing::info!(target: "my_target" , "hello my target" ); |
73 | |
74 | unfiltered_handle.assert_finished(); |
75 | debug_handle.assert_finished(); |
76 | target_handle.assert_finished(); |
77 | } |
78 | |
79 | #[test] |
80 | fn mixed_max_level_hint() { |
81 | let unfiltered = layer::named("unfiltered" ).run().boxed(); |
82 | let info = layer::named("info" ) |
83 | .run() |
84 | .with_filter(LevelFilter::INFO) |
85 | .boxed(); |
86 | let debug = layer::named("debug" ) |
87 | .run() |
88 | .with_filter(LevelFilter::DEBUG) |
89 | .boxed(); |
90 | |
91 | let subscriber = tracing_subscriber::registry().with(vec![unfiltered, info, debug]); |
92 | |
93 | assert_eq!(subscriber.max_level_hint(), None); |
94 | } |
95 | |
96 | #[test] |
97 | fn all_filtered_max_level_hint() { |
98 | let warn = layer::named("warn" ) |
99 | .run() |
100 | .with_filter(LevelFilter::WARN) |
101 | .boxed(); |
102 | let info = layer::named("info" ) |
103 | .run() |
104 | .with_filter(LevelFilter::INFO) |
105 | .boxed(); |
106 | let debug = layer::named("debug" ) |
107 | .run() |
108 | .with_filter(LevelFilter::DEBUG) |
109 | .boxed(); |
110 | |
111 | let subscriber = tracing_subscriber::registry().with(vec![warn, info, debug]); |
112 | |
113 | assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG)); |
114 | } |
115 | |
116 | #[test] |
117 | fn empty_vec() { |
118 | // Just a None means everything is off |
119 | let subscriber = tracing_subscriber::registry().with(Vec::<MockLayer>::new()); |
120 | assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF)); |
121 | } |
122 | |