tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.52k stars 725 forks source link

Documentation examples of `tracing_subscriber::reload` do not work as described #3085

Open bluenote10 opened 2 months ago

bluenote10 commented 2 months ago

Bug Report

I'm not sure if this is an actual (software) bug, or just a case of an outdated documentation: The examples given in the documentation of tracing_subscriber::reload do not seem to work as described there.

Version

│   │       │   │   └── tracing v0.1.40
│   │       │   │       ├── tracing-attributes v0.1.27 (proc-macro)
│   │       │   │       └── tracing-core v0.1.32
│   │       ├── tracing v0.1.40 (*)
│   ├── tracing v0.1.40 (*)
│   ├── tracing-subscriber v0.3.18
│   │   ├── tracing v0.1.40 (*)
│   │   ├── tracing-core v0.1.32 (*)
│   │   └── tracing-log v0.2.0
│   │       └── tracing-core v0.1.32 (*)
└── tracing-subscriber v0.3.18 (*)

The log crate is 0.4.22.

Platform

Linux fabuntu 5.15.0-122-generic #132-Ubuntu SMP Thu Aug 29 13:45:52 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux

Crates

tracing_subscriber

Description

I've copy/pasted the two examples given in the documentation into this standalone module, extended by a bit more debug output:

use log::{info, warn};
use tracing_subscriber::{filter, fmt, prelude::*, reload};

fn example_1() {
    let filter = filter::LevelFilter::WARN;
    let (filter, reload_handle) = reload::Layer::new(filter);
    tracing_subscriber::registry()
        .with(filter)
        .with(fmt::Layer::default())
        .init();
    info!("This will be ignored");
    reload_handle
        .modify(|filter| *filter = filter::LevelFilter::INFO)
        .unwrap();
    warn!("Test line before");
    info!("This will be logged");
    warn!("Test line after");
}

fn example_2() {
    let filtered_layer = fmt::Layer::default().with_filter(filter::LevelFilter::WARN);
    let (filtered_layer, reload_handle) = reload::Layer::new(filtered_layer);
    tracing_subscriber::registry().with(filtered_layer).init();
    info!("This will be ignored");
    reload_handle
        .modify(|layer| *layer.filter_mut() = filter::LevelFilter::INFO)
        .unwrap();
    warn!("Test line before");
    info!("This will be logged");
    warn!("Test line after");
}

fn main() {
    example_1();
}

Running either example_1() or example_2() both result in the output:

2024-09-22T08:02:05.237190Z  WARN debug_logging: Test line before
2024-09-22T08:02:05.237210Z  WARN debug_logging: Test line after

I.e., both examples don't actually log the line info!("This will be logged"); as they suggest to do.

Possibly related to:

kaffarell commented 3 weeks ago

yes, this looks like a duplicate of #2711, which has already been fixed on master with #1270, but hasn't been backported for some reason. Note that it works with the tracing::info!, tracing::warn! macros.