1 | // These tests include field filters with no targets, so they have to go in a |
2 | // separate file. |
3 | #![cfg (feature = "env-filter" )] |
4 | |
5 | use tracing::{self, subscriber::with_default, Level}; |
6 | use tracing_mock::*; |
7 | use tracing_subscriber::{filter::EnvFilter, prelude::*}; |
8 | |
9 | #[test] |
10 | fn same_length_targets() { |
11 | let filter: EnvFilter = "foo=trace,bar=trace" .parse().expect("filter should parse" ); |
12 | let (subscriber, finished) = subscriber::mock() |
13 | .event(expect::event().at_level(Level::TRACE)) |
14 | .event(expect::event().at_level(Level::TRACE)) |
15 | .only() |
16 | .run_with_handle(); |
17 | let subscriber = subscriber.with(filter); |
18 | |
19 | with_default(subscriber, || { |
20 | tracing::trace!(target: "foo" , "foo" ); |
21 | tracing::trace!(target: "bar" , "bar" ); |
22 | }); |
23 | |
24 | finished.assert_finished(); |
25 | } |
26 | |
27 | #[test] |
28 | fn same_num_fields_event() { |
29 | let filter: EnvFilter = "[{foo}]=trace,[{bar}]=trace" |
30 | .parse() |
31 | .expect("filter should parse" ); |
32 | let (subscriber, finished) = subscriber::mock() |
33 | .event( |
34 | expect::event() |
35 | .at_level(Level::TRACE) |
36 | .with_fields(expect::field("foo" )), |
37 | ) |
38 | .event( |
39 | expect::event() |
40 | .at_level(Level::TRACE) |
41 | .with_fields(expect::field("bar" )), |
42 | ) |
43 | .only() |
44 | .run_with_handle(); |
45 | let subscriber = subscriber.with(filter); |
46 | with_default(subscriber, || { |
47 | tracing::trace!(foo = 1); |
48 | tracing::trace!(bar = 3); |
49 | }); |
50 | |
51 | finished.assert_finished(); |
52 | } |
53 | |
54 | #[test] |
55 | fn same_num_fields_and_name_len() { |
56 | let filter: EnvFilter = "[foo{bar=1}]=trace,[baz{boz=1}]=trace" |
57 | .parse() |
58 | .expect("filter should parse" ); |
59 | let (subscriber, finished) = subscriber::mock() |
60 | .new_span( |
61 | expect::span() |
62 | .named("foo" ) |
63 | .at_level(Level::TRACE) |
64 | .with_field(expect::field("bar" )), |
65 | ) |
66 | .new_span( |
67 | expect::span() |
68 | .named("baz" ) |
69 | .at_level(Level::TRACE) |
70 | .with_field(expect::field("boz" )), |
71 | ) |
72 | .only() |
73 | .run_with_handle(); |
74 | let subscriber = subscriber.with(filter); |
75 | with_default(subscriber, || { |
76 | tracing::trace_span!("foo" , bar = 1); |
77 | tracing::trace_span!("baz" , boz = 1); |
78 | }); |
79 | |
80 | finished.assert_finished(); |
81 | } |
82 | |