summaryrefslogtreecommitdiffstats
path: root/third_party/rust/tracing/tests/max_level_hint.rs
blob: 63d3af635705ff8fa37701d7d743d83fadb701b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use tracing::Level;
use tracing_mock::*;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[test]
fn max_level_hints() {
    // This test asserts that when a subscriber provides us with the global
    // maximum level that it will enable (by implementing the
    // `Subscriber::max_level_hint` method), we will never call
    // `Subscriber::enabled` for events above that maximum level.
    //
    // In this case, we test that by making the `enabled` method assert that no
    // `Metadata` for spans or events at the `TRACE` or `DEBUG` levels.
    let (subscriber, handle) = subscriber::mock()
        .with_max_level_hint(Level::INFO)
        .with_filter(|meta| {
            assert!(
                dbg!(meta).level() <= &Level::INFO,
                "a TRACE or DEBUG event was dynamically filtered: "
            );
            true
        })
        .event(event::mock().at_level(Level::INFO))
        .event(event::mock().at_level(Level::WARN))
        .event(event::mock().at_level(Level::ERROR))
        .done()
        .run_with_handle();

    tracing::subscriber::set_global_default(subscriber).unwrap();

    tracing::info!("doing a thing that you might care about");
    tracing::debug!("charging turboencabulator with interocitor");
    tracing::warn!("extremely serious warning, pay attention");
    tracing::trace!("interocitor charge level is 10%");
    tracing::error!("everything is on fire");
    handle.assert_finished();
}