1 | #![cfg (feature = "registry" )] |
2 | mod boxed; |
3 | mod downcast_raw; |
4 | mod filter_scopes; |
5 | mod option; |
6 | mod per_event; |
7 | mod targets; |
8 | mod trees; |
9 | mod vec; |
10 | |
11 | use tracing::{level_filters::LevelFilter, Level}; |
12 | use tracing_mock::{event, expect, layer, subscriber}; |
13 | use tracing_subscriber::{filter, prelude::*, Layer}; |
14 | |
15 | #[test] |
16 | fn basic_layer_filters() { |
17 | let (trace_layer, trace_handle) = layer::named("trace" ) |
18 | .event(expect::event().at_level(Level::TRACE)) |
19 | .event(expect::event().at_level(Level::DEBUG)) |
20 | .event(expect::event().at_level(Level::INFO)) |
21 | .only() |
22 | .run_with_handle(); |
23 | |
24 | let (debug_layer, debug_handle) = layer::named("debug" ) |
25 | .event(expect::event().at_level(Level::DEBUG)) |
26 | .event(expect::event().at_level(Level::INFO)) |
27 | .only() |
28 | .run_with_handle(); |
29 | |
30 | let (info_layer, info_handle) = layer::named("info" ) |
31 | .event(expect::event().at_level(Level::INFO)) |
32 | .only() |
33 | .run_with_handle(); |
34 | |
35 | let _subscriber = tracing_subscriber::registry() |
36 | .with(trace_layer.with_filter(LevelFilter::TRACE)) |
37 | .with(debug_layer.with_filter(LevelFilter::DEBUG)) |
38 | .with(info_layer.with_filter(LevelFilter::INFO)) |
39 | .set_default(); |
40 | |
41 | tracing::trace!("hello trace" ); |
42 | tracing::debug!("hello debug" ); |
43 | tracing::info!("hello info" ); |
44 | |
45 | trace_handle.assert_finished(); |
46 | debug_handle.assert_finished(); |
47 | info_handle.assert_finished(); |
48 | } |
49 | |
50 | #[test] |
51 | fn basic_layer_filter_spans() { |
52 | let (trace_layer, trace_handle) = layer::named("trace" ) |
53 | .new_span(expect::span().at_level(Level::TRACE)) |
54 | .new_span(expect::span().at_level(Level::DEBUG)) |
55 | .new_span(expect::span().at_level(Level::INFO)) |
56 | .only() |
57 | .run_with_handle(); |
58 | |
59 | let (debug_layer, debug_handle) = layer::named("debug" ) |
60 | .new_span(expect::span().at_level(Level::DEBUG)) |
61 | .new_span(expect::span().at_level(Level::INFO)) |
62 | .only() |
63 | .run_with_handle(); |
64 | |
65 | let (info_layer, info_handle) = layer::named("info" ) |
66 | .new_span(expect::span().at_level(Level::INFO)) |
67 | .only() |
68 | .run_with_handle(); |
69 | |
70 | let _subscriber = tracing_subscriber::registry() |
71 | .with(trace_layer.with_filter(LevelFilter::TRACE)) |
72 | .with(debug_layer.with_filter(LevelFilter::DEBUG)) |
73 | .with(info_layer.with_filter(LevelFilter::INFO)) |
74 | .set_default(); |
75 | |
76 | tracing::trace_span!("hello trace" ); |
77 | tracing::debug_span!("hello debug" ); |
78 | tracing::info_span!("hello info" ); |
79 | |
80 | trace_handle.assert_finished(); |
81 | debug_handle.assert_finished(); |
82 | info_handle.assert_finished(); |
83 | } |
84 | |
85 | #[test] |
86 | fn global_filters_subscribers_still_work() { |
87 | let (expect, handle) = layer::mock() |
88 | .event(expect::event().at_level(Level::INFO)) |
89 | .event(expect::event().at_level(Level::WARN)) |
90 | .event(expect::event().at_level(Level::ERROR)) |
91 | .only() |
92 | .run_with_handle(); |
93 | |
94 | let _subscriber = tracing_subscriber::registry() |
95 | .with(expect) |
96 | .with(LevelFilter::INFO) |
97 | .set_default(); |
98 | |
99 | tracing::trace!("hello trace" ); |
100 | tracing::debug!("hello debug" ); |
101 | tracing::info!("hello info" ); |
102 | tracing::warn!("hello warn" ); |
103 | tracing::error!("hello error" ); |
104 | |
105 | handle.assert_finished(); |
106 | } |
107 | |
108 | #[test] |
109 | fn global_filter_interests_are_cached() { |
110 | let (expect, handle) = layer::mock() |
111 | .event(expect::event().at_level(Level::WARN)) |
112 | .event(expect::event().at_level(Level::ERROR)) |
113 | .only() |
114 | .run_with_handle(); |
115 | |
116 | let _subscriber = tracing_subscriber::registry() |
117 | .with(expect.with_filter(filter::filter_fn(|meta| { |
118 | assert!( |
119 | meta.level() <= &Level::INFO, |
120 | "enabled should not be called for callsites disabled by the global filter" |
121 | ); |
122 | meta.level() <= &Level::WARN |
123 | }))) |
124 | .with(LevelFilter::INFO) |
125 | .set_default(); |
126 | |
127 | tracing::trace!("hello trace" ); |
128 | tracing::debug!("hello debug" ); |
129 | tracing::info!("hello info" ); |
130 | tracing::warn!("hello warn" ); |
131 | tracing::error!("hello error" ); |
132 | |
133 | handle.assert_finished(); |
134 | } |
135 | |
136 | #[test] |
137 | fn global_filters_affect_subscriber_filters() { |
138 | let (expect, handle) = layer::named("debug" ) |
139 | .event(expect::event().at_level(Level::INFO)) |
140 | .event(expect::event().at_level(Level::WARN)) |
141 | .event(expect::event().at_level(Level::ERROR)) |
142 | .only() |
143 | .run_with_handle(); |
144 | |
145 | let _subscriber = tracing_subscriber::registry() |
146 | .with(expect.with_filter(LevelFilter::DEBUG)) |
147 | .with(LevelFilter::INFO) |
148 | .set_default(); |
149 | |
150 | tracing::trace!("hello trace" ); |
151 | tracing::debug!("hello debug" ); |
152 | tracing::info!("hello info" ); |
153 | tracing::warn!("hello warn" ); |
154 | tracing::error!("hello error" ); |
155 | |
156 | handle.assert_finished(); |
157 | } |
158 | |
159 | #[test] |
160 | fn filter_fn() { |
161 | let (all, all_handle) = layer::named("all_targets" ) |
162 | .event(event::msg("hello foo" )) |
163 | .event(event::msg("hello bar" )) |
164 | .only() |
165 | .run_with_handle(); |
166 | |
167 | let (foo, foo_handle) = layer::named("foo_target" ) |
168 | .event(event::msg("hello foo" )) |
169 | .only() |
170 | .run_with_handle(); |
171 | |
172 | let (bar, bar_handle) = layer::named("bar_target" ) |
173 | .event(event::msg("hello bar" )) |
174 | .only() |
175 | .run_with_handle(); |
176 | |
177 | let _subscriber = tracing_subscriber::registry() |
178 | .with(all) |
179 | .with(foo.with_filter(filter::filter_fn(|meta| meta.target().starts_with("foo" )))) |
180 | .with(bar.with_filter(filter::filter_fn(|meta| meta.target().starts_with("bar" )))) |
181 | .set_default(); |
182 | |
183 | tracing::trace!(target: "foo" , "hello foo" ); |
184 | tracing::trace!(target: "bar" , "hello bar" ); |
185 | |
186 | foo_handle.assert_finished(); |
187 | bar_handle.assert_finished(); |
188 | all_handle.assert_finished(); |
189 | } |
190 | |