From 1e382a172fc08e03ac2330e9a2932f2d0fc185d1 Mon Sep 17 00:00:00 2001 From: Xiangfei Ding Date: Sun, 12 Oct 2025 18:29:46 +0000 Subject: [PATCH] move EnvFilter into its own layer `tracing` at the time of writing has a feature (?) in its Filter implementation, so that filters like EnvFilter are consulted for status of a span or event and whether it is marked as interesting for logging. Combining a Filter with another layer through the `with_filter` combinator produces a filtered layer that enables an event unless it is statically determined that the event is uninteresting. However, if the filter is dynamic, because of filtering on span names or field values as an example, events are **always** enabled. There is an `event_enabled` predicate on `EnvFilter` implementation but it falls back to default and, thus, the dynamic filters are **unused**. This patch re-enables span- and field-based filters. --- compiler/rustc_log/src/lib.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_log/src/lib.rs b/compiler/rustc_log/src/lib.rs index 26475eec1c102..8e72022671ce5 100644 --- a/compiler/rustc_log/src/lib.rs +++ b/compiler/rustc_log/src/lib.rs @@ -39,11 +39,11 @@ use std::io::{self, IsTerminal}; use tracing::dispatcher::SetGlobalDefaultError; use tracing::{Event, Subscriber}; +use tracing_subscriber::Registry; use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter}; use tracing_subscriber::fmt::FmtContext; use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields}; use tracing_subscriber::layer::SubscriberExt; -use tracing_subscriber::{Layer, Registry}; /// The values of all the environment variables that matter for configuring a logger. /// Errors are explicitly preserved so that we can share error handling. @@ -155,18 +155,19 @@ where Err(_) => {} // no wraptree } - let subscriber = build_subscriber().with(layer.with_filter(filter)); + let subscriber = build_subscriber(); + // NOTE: It is important to make sure that the filter is applied on the last layer match cfg.backtrace { Ok(backtrace_target) => { let fmt_layer = tracing_subscriber::fmt::layer() .with_writer(io::stderr) .without_time() .event_format(BacktraceFormatter { backtrace_target }); - let subscriber = subscriber.with(fmt_layer); + let subscriber = subscriber.with(layer).with(fmt_layer).with(filter); tracing::subscriber::set_global_default(subscriber)?; } Err(_) => { - tracing::subscriber::set_global_default(subscriber)?; + tracing::subscriber::set_global_default(subscriber.with(layer).with(filter))?; } };