1 | use super::*; |
2 | use tracing_mock::{event, expect, layer::MockLayer}; |
3 | |
4 | #[test] |
5 | fn basic_trees() { |
6 | let (with_target, with_target_handle) = layer::named("info_with_target" ) |
7 | .event( |
8 | expect::event() |
9 | .at_level(Level::INFO) |
10 | .with_target("my_target" ), |
11 | ) |
12 | .only() |
13 | .run_with_handle(); |
14 | |
15 | let (info, info_handle) = layer::named("info" ) |
16 | .event( |
17 | expect::event() |
18 | .at_level(Level::INFO) |
19 | .with_target(module_path!()), |
20 | ) |
21 | .event( |
22 | expect::event() |
23 | .at_level(Level::INFO) |
24 | .with_target("my_target" ), |
25 | ) |
26 | .only() |
27 | .run_with_handle(); |
28 | |
29 | let (all, all_handle) = layer::named("all" ) |
30 | .event( |
31 | expect::event() |
32 | .at_level(Level::INFO) |
33 | .with_target(module_path!()), |
34 | ) |
35 | .event(expect::event().at_level(Level::TRACE)) |
36 | .event( |
37 | expect::event() |
38 | .at_level(Level::INFO) |
39 | .with_target("my_target" ), |
40 | ) |
41 | .event( |
42 | expect::event() |
43 | .at_level(Level::TRACE) |
44 | .with_target("my_target" ), |
45 | ) |
46 | .only() |
47 | .run_with_handle(); |
48 | |
49 | let info_tree = info |
50 | .and_then( |
51 | with_target.with_filter(filter::filter_fn(|meta| dbg!(meta.target()) == "my_target" )), |
52 | ) |
53 | .with_filter(LevelFilter::INFO); |
54 | |
55 | let subscriber = tracing_subscriber::registry().with(info_tree).with(all); |
56 | let _guard = dbg!(subscriber).set_default(); |
57 | |
58 | tracing::info!("hello world" ); |
59 | tracing::trace!("hello trace" ); |
60 | tracing::info!(target: "my_target" , "hi to my target" ); |
61 | tracing::trace!(target: "my_target" , "hi to my target at trace" ); |
62 | |
63 | all_handle.assert_finished(); |
64 | info_handle.assert_finished(); |
65 | with_target_handle.assert_finished(); |
66 | } |
67 | |
68 | #[test] |
69 | fn filter_span_scopes() { |
70 | fn target_layer(target: &'static str) -> (MockLayer, subscriber::MockHandle) { |
71 | layer::named(format!("target_{}" , target)) |
72 | .enter(expect::span().with_target(target).at_level(Level::INFO)) |
73 | .event(event::msg("hello world" ).in_scope(vec![ |
74 | expect::span().with_target(target).at_level(Level::INFO), |
75 | ])) |
76 | .exit(expect::span().with_target(target).at_level(Level::INFO)) |
77 | .only() |
78 | .run_with_handle() |
79 | } |
80 | |
81 | let (a_layer, a_handle) = target_layer("a" ); |
82 | let (b_layer, b_handle) = target_layer("b" ); |
83 | let (info_layer, info_handle) = layer::named("info" ) |
84 | .enter(expect::span().with_target("b" ).at_level(Level::INFO)) |
85 | .enter(expect::span().with_target("a" ).at_level(Level::INFO)) |
86 | .event(event::msg("hello world" ).in_scope(vec![ |
87 | expect::span().with_target("a" ).at_level(Level::INFO), |
88 | expect::span().with_target("b" ).at_level(Level::INFO), |
89 | ])) |
90 | .exit(expect::span().with_target("a" ).at_level(Level::INFO)) |
91 | .exit(expect::span().with_target("b" ).at_level(Level::INFO)) |
92 | .only() |
93 | .run_with_handle(); |
94 | |
95 | let full_scope = vec![ |
96 | expect::span().with_target("b" ).at_level(Level::TRACE), |
97 | expect::span().with_target("a" ).at_level(Level::INFO), |
98 | expect::span().with_target("b" ).at_level(Level::INFO), |
99 | expect::span().with_target("a" ).at_level(Level::TRACE), |
100 | ]; |
101 | let (all_layer, all_handle) = layer::named("all" ) |
102 | .enter(expect::span().with_target("a" ).at_level(Level::TRACE)) |
103 | .enter(expect::span().with_target("b" ).at_level(Level::INFO)) |
104 | .enter(expect::span().with_target("a" ).at_level(Level::INFO)) |
105 | .enter(expect::span().with_target("b" ).at_level(Level::TRACE)) |
106 | .event(event::msg("hello world" ).in_scope(full_scope.clone())) |
107 | .event( |
108 | event::msg("hello to my target" ) |
109 | .with_target("a" ) |
110 | .in_scope(full_scope.clone()), |
111 | ) |
112 | .event( |
113 | event::msg("hello to my target" ) |
114 | .with_target("b" ) |
115 | .in_scope(full_scope), |
116 | ) |
117 | .exit(expect::span().with_target("b" ).at_level(Level::TRACE)) |
118 | .exit(expect::span().with_target("a" ).at_level(Level::INFO)) |
119 | .exit(expect::span().with_target("b" ).at_level(Level::INFO)) |
120 | .exit(expect::span().with_target("a" ).at_level(Level::TRACE)) |
121 | .only() |
122 | .run_with_handle(); |
123 | |
124 | let a_layer = a_layer.with_filter(filter::filter_fn(|meta| { |
125 | let target = meta.target(); |
126 | target == "a" || target == module_path!() |
127 | })); |
128 | |
129 | let b_layer = b_layer.with_filter(filter::filter_fn(|meta| { |
130 | let target = meta.target(); |
131 | target == "b" || target == module_path!() |
132 | })); |
133 | |
134 | let info_tree = info_layer |
135 | .and_then(a_layer) |
136 | .and_then(b_layer) |
137 | .with_filter(LevelFilter::INFO); |
138 | |
139 | let subscriber = tracing_subscriber::registry() |
140 | .with(info_tree) |
141 | .with(all_layer); |
142 | let _guard = dbg!(subscriber).set_default(); |
143 | |
144 | { |
145 | let _a1 = tracing::trace_span!(target: "a" , "a/trace" ).entered(); |
146 | let _b1 = tracing::info_span!(target: "b" , "b/info" ).entered(); |
147 | let _a2 = tracing::info_span!(target: "a" , "a/info" ).entered(); |
148 | let _b2 = tracing::trace_span!(target: "b" , "b/trace" ).entered(); |
149 | tracing::info!("hello world" ); |
150 | tracing::debug!(target: "a" , "hello to my target" ); |
151 | tracing::debug!(target: "b" , "hello to my target" ); |
152 | } |
153 | |
154 | all_handle.assert_finished(); |
155 | info_handle.assert_finished(); |
156 | a_handle.assert_finished(); |
157 | b_handle.assert_finished(); |
158 | } |
159 | |