Skip to content

Commit

Permalink
subscriber: fix max_level_hint for empty Option/Vec Layer imp…
Browse files Browse the repository at this point in the history
…ls (#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>
  • Loading branch information
guswynn and hawkw committed Jul 1, 2022
1 parent 6d30a01 commit d864366
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
9 changes: 7 additions & 2 deletions tracing-subscriber/src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1490,7 +1490,11 @@ where
fn max_level_hint(&self) -> Option<LevelFilter> {
match self {
Some(ref inner) => inner.max_level_hint(),
None => None,
None => {
// There is no inner layer, so this layer will
// never enable anything.
Some(LevelFilter::OFF)
}
}
}

Expand Down Expand Up @@ -1701,7 +1705,8 @@ feature! {
}

fn max_level_hint(&self) -> Option<LevelFilter> {
let mut max_level = LevelFilter::ERROR;
// Default to `OFF` if there are no inner layers.
let mut max_level = LevelFilter::OFF;
for l in self {
// NOTE(eliza): this is slightly subtle: if *any* layer
// returns `None`, we have to return `None`, assuming there is
Expand Down
7 changes: 7 additions & 0 deletions tracing-subscriber/tests/layer_filters/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,10 @@ fn all_filtered_max_level_hint() {

assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));
}

#[test]
fn empty_vec() {
// Just a None means everything is off
let subscriber = tracing_subscriber::registry().with(Vec::<ExpectLayer>::new());
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF));
}
41 changes: 41 additions & 0 deletions tracing-subscriber/tests/option.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![cfg(feature = "registry")]
use tracing::level_filters::LevelFilter;
use tracing::Subscriber;
use tracing_subscriber::prelude::*;

// This test is just used to compare to the tests below
#[test]
fn just_layer() {
let subscriber = tracing_subscriber::registry().with(LevelFilter::INFO);
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::INFO));
}

#[test]
fn subscriber_and_option_some_layer() {
let subscriber = tracing_subscriber::registry()
.with(LevelFilter::INFO)
.with(Some(LevelFilter::DEBUG));
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::DEBUG));
}

#[test]
fn subscriber_and_option_none_layer() {
// None means the other layer takes control
let subscriber = tracing_subscriber::registry()
.with(LevelFilter::ERROR)
.with(None::<LevelFilter>);
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::ERROR));
}

#[test]
fn just_option_some_layer() {
// Just a None means everything is off
let subscriber = tracing_subscriber::registry().with(None::<LevelFilter>);
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF));
}

#[test]
fn just_option_none_layer() {
let subscriber = tracing_subscriber::registry().with(Some(LevelFilter::ERROR));
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::ERROR));
}
19 changes: 19 additions & 0 deletions tracing-subscriber/tests/vec.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![cfg(feature = "registry")]
use tracing::level_filters::LevelFilter;
use tracing::Subscriber;
use tracing_subscriber::prelude::*;

#[test]
fn just_empty_vec() {
// Just a None means everything is off
let subscriber = tracing_subscriber::registry().with(Vec::<LevelFilter>::new());
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::OFF));
}

#[test]
fn layer_and_empty_vec() {
let subscriber = tracing_subscriber::registry()
.with(LevelFilter::INFO)
.with(Vec::<LevelFilter>::new());
assert_eq!(subscriber.max_level_hint(), Some(LevelFilter::INFO));
}

0 comments on commit d864366

Please sign in to comment.