Skip to content

Commit 3d2f486

Browse files
guswynnhawkw
authored andcommitted
subscriber: fix max_level_hint for empty Option/Vec Layer impls (tokio-rs#2195)
## Motivation These are incorrect: currently, when you have a `None` layer, the `None` hint it returns causes the default `TRACE` to win, which is inaccurate. Similarly, `Vec` should default to `OFF` if it has no `Layer`s in it ## Solution Change the default hints to `Some(OFF)` Co-authored-by: Eliza Weisman <eliza@buoyant.io>
1 parent 8bdd972 commit 3d2f486

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

tracing-subscriber/src/layer/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,11 @@ where
14901490
fn max_level_hint(&self) -> Option<LevelFilter> {
14911491
match self {
14921492
Some(ref inner) => inner.max_level_hint(),
1493-
None => None,
1493+
None => {
1494+
// There is no inner layer, so this layer will
1495+
// never enable anything.
1496+
Some(LevelFilter::OFF)
1497+
}
14941498
}
14951499
}
14961500

@@ -1701,7 +1705,8 @@ feature! {
17011705
}
17021706

17031707
fn max_level_hint(&self) -> Option<LevelFilter> {
1704-
let mut max_level = LevelFilter::ERROR;
1708+
// Default to `OFF` if there are no inner layers.
1709+
let mut max_level = LevelFilter::OFF;
17051710
for l in self {
17061711
// NOTE(eliza): this is slightly subtle: if *any* layer
17071712
// returns `None`, we have to return `None`, assuming there is

tracing-subscriber/tests/layer_filters/vec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,10 @@ fn all_filtered_max_level_hint() {
111111

112112
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));
113113
}
114+
115+
#[test]
116+
fn empty_vec() {
117+
// Just a None means everything is off
118+
let subscriber = tracing_subscriber::registry().with(Vec::<ExpectLayer>::new());
119+
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF));
120+
}

0 commit comments

Comments
 (0)