tokio-rs / tracing

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

tracing-subscriber: `Filtered` does not respect global filters properly #2860

Open jstarks opened 6 months ago

jstarks commented 6 months ago

Bug Report

Version

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

Description

The Filtered type unconditionally returns Interest::always from register_callsite(). As a result, if the Filtered type is filtering over a global filter, the global filter has no effect. This seems undesirable. It should either return Interest::sometimes so that enabled is called for each event, or (preferred, for performance) it should pass through to its inner layer's register_callsite() method.

Repro:

use tracing_subscriber::filter::FilterFn;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::Layer;

fn main() {
    let disable_all = FilterFn::new(|_| false);
    let enable_all = FilterFn::new(|_| true);

    tracing_subscriber::registry()
        .with(
            Layer::and_then(disable_all, tracing_subscriber::fmt::layer()).with_filter(enable_all),
        )
        .init();

    tracing::info!("you had better not see this");
}

Expected:

[no output]

Actual:

2024-01-24T21:50:12.178070Z  INFO bug: you had better not see this