Skip to content

Commit 2c568b5

Browse files
guswynnhawkw
andauthored
subscriber: fix max_level_hint for empty Option/Vec Subscribe impls (#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 `Subscribe`s in it ## Solution Change the default hints to `Some(OFF)` Co-authored-by: Eliza Weisman <eliza@buoyant.io>
1 parent 665ad11 commit 2c568b5

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

tracing-subscriber/src/subscribe/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,7 +1485,11 @@ where
14851485
fn max_level_hint(&self) -> Option<LevelFilter> {
14861486
match self {
14871487
Some(ref inner) => inner.max_level_hint(),
1488-
None => None,
1488+
None => {
1489+
// There is no inner subscriber, so this subscriber will
1490+
// never enable anything.
1491+
Some(LevelFilter::OFF)
1492+
}
14891493
}
14901494
}
14911495

@@ -1690,7 +1694,8 @@ feature! {
16901694
}
16911695

16921696
fn max_level_hint(&self) -> Option<LevelFilter> {
1693-
let mut max_level = LevelFilter::ERROR;
1697+
// Default to `OFF` if there are no underlying subscribers
1698+
let mut max_level = LevelFilter::OFF;
16941699
for s in self {
16951700
// NOTE(eliza): this is slightly subtle: if *any* subscriber
16961701
// returns `None`, we have to return `None`, assuming there is

tracing-subscriber/tests/option.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#![cfg(feature = "registry")]
2+
use tracing::level_filters::LevelFilter;
3+
use tracing::Collect;
4+
use tracing_subscriber::prelude::*;
5+
6+
// This test is just used to compare to the tests below
7+
#[test]
8+
fn just_subscriber() {
9+
let collector = tracing_subscriber::registry().with(LevelFilter::INFO);
10+
assert_eq!(collector.max_level_hint(), Some(LevelFilter::INFO));
11+
}
12+
13+
#[test]
14+
fn subscriber_and_option_some_subscriber() {
15+
let collector = tracing_subscriber::registry()
16+
.with(LevelFilter::INFO)
17+
.with(Some(LevelFilter::DEBUG));
18+
assert_eq!(collector.max_level_hint(), Some(LevelFilter::DEBUG));
19+
}
20+
21+
#[test]
22+
fn subscriber_and_option_none_subscriber() {
23+
// None means the other layer takes control
24+
let collector = tracing_subscriber::registry()
25+
.with(LevelFilter::ERROR)
26+
.with(None::<LevelFilter>);
27+
assert_eq!(collector.max_level_hint(), Some(LevelFilter::ERROR));
28+
}
29+
30+
#[test]
31+
fn just_option_some_subscriber() {
32+
// Just a None means everything is off
33+
let collector = tracing_subscriber::registry().with(None::<LevelFilter>);
34+
assert_eq!(collector.max_level_hint(), Some(LevelFilter::OFF));
35+
}
36+
37+
#[test]
38+
fn just_option_none_subscriber() {
39+
let collector = tracing_subscriber::registry().with(Some(LevelFilter::ERROR));
40+
assert_eq!(collector.max_level_hint(), Some(LevelFilter::ERROR));
41+
}

tracing-subscriber/tests/subscriber_filters/vec.rs

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

116116
assert_eq!(collector.max_level_hint(), Some(LevelFilter::DEBUG));
117117
}
118+
119+
#[test]
120+
fn empty_vec() {
121+
// Just a None means everything is off
122+
let collector = tracing_subscriber::registry().with(Vec::<ExpectSubscriber>::new());
123+
assert_eq!(collector.max_level_hint(), Some(LevelFilter::OFF));
124+
}

tracing-subscriber/tests/vec.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![cfg(feature = "registry")]
2+
use tracing::level_filters::LevelFilter;
3+
use tracing::Collect;
4+
use tracing_subscriber::prelude::*;
5+
6+
#[test]
7+
fn just_empty_vec() {
8+
// Just a None means everything is off
9+
let collector = tracing_subscriber::registry().with(Vec::<LevelFilter>::new());
10+
assert_eq!(collector.max_level_hint(), Some(LevelFilter::OFF));
11+
}
12+
13+
#[test]
14+
fn subscriber_and_empty_vec() {
15+
let collector = tracing_subscriber::registry()
16+
.with(LevelFilter::INFO)
17+
.with(Vec::<LevelFilter>::new());
18+
assert_eq!(collector.max_level_hint(), Some(LevelFilter::INFO));
19+
}

0 commit comments

Comments
 (0)