1#![cfg(all(feature = "env-filter", feature = "tracing-log"))]
2
3use tracing::{self, Level};
4use tracing_mock::*;
5use tracing_subscriber::{filter::EnvFilter, prelude::*};
6
7mod my_module {
8 pub(crate) fn test_records() {
9 log::trace!("this should be disabled");
10 log::info!("this shouldn't be");
11 log::debug!("this should be disabled");
12 log::warn!("this should be enabled");
13 log::warn!(target: "something else", "this shouldn't be enabled");
14 log::error!("this should be enabled too");
15 }
16
17 pub(crate) fn test_log_enabled() {
18 assert!(
19 log::log_enabled!(log::Level::Info),
20 "info should be enabled inside `my_module`"
21 );
22 assert!(
23 !log::log_enabled!(log::Level::Debug),
24 "debug should not be enabled inside `my_module`"
25 );
26 assert!(
27 log::log_enabled!(log::Level::Warn),
28 "warn should be enabled inside `my_module`"
29 );
30 }
31}
32
33#[test]
34fn log_is_enabled() {
35 let filter: EnvFilter = "filter_log::my_module=info"
36 .parse()
37 .expect("filter should parse");
38 let (subscriber, finished) = subscriber::mock()
39 .event(expect::event().at_level(Level::INFO))
40 .event(expect::event().at_level(Level::WARN))
41 .event(expect::event().at_level(Level::ERROR))
42 .only()
43 .run_with_handle();
44
45 // Note: we have to set the global default in order to set the `log` max
46 // level, which can only be set once.
47 subscriber.with(filter).init();
48
49 my_module::test_records();
50 log::info!("this is disabled");
51
52 my_module::test_log_enabled();
53 assert!(
54 !log::log_enabled!(log::Level::Info),
55 "info should not be enabled outside `my_module`"
56 );
57 assert!(
58 !log::log_enabled!(log::Level::Warn),
59 "warn should not be enabled outside `my_module`"
60 );
61
62 finished.assert_finished();
63}
64