Skip to content

Commit 1e382a1

Browse files
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.
1 parent ff6dc92 commit 1e382a1

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

compiler/rustc_log/src/lib.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ use std::io::{self, IsTerminal};
3939

4040
use tracing::dispatcher::SetGlobalDefaultError;
4141
use tracing::{Event, Subscriber};
42+
use tracing_subscriber::Registry;
4243
use tracing_subscriber::filter::{Directive, EnvFilter, LevelFilter};
4344
use tracing_subscriber::fmt::FmtContext;
4445
use tracing_subscriber::fmt::format::{self, FormatEvent, FormatFields};
4546
use tracing_subscriber::layer::SubscriberExt;
46-
use tracing_subscriber::{Layer, Registry};
4747

4848
/// The values of all the environment variables that matter for configuring a logger.
4949
/// Errors are explicitly preserved so that we can share error handling.
@@ -155,18 +155,19 @@ where
155155
Err(_) => {} // no wraptree
156156
}
157157

158-
let subscriber = build_subscriber().with(layer.with_filter(filter));
158+
let subscriber = build_subscriber();
159+
// NOTE: It is important to make sure that the filter is applied on the last layer
159160
match cfg.backtrace {
160161
Ok(backtrace_target) => {
161162
let fmt_layer = tracing_subscriber::fmt::layer()
162163
.with_writer(io::stderr)
163164
.without_time()
164165
.event_format(BacktraceFormatter { backtrace_target });
165-
let subscriber = subscriber.with(fmt_layer);
166+
let subscriber = subscriber.with(layer).with(fmt_layer).with(filter);
166167
tracing::subscriber::set_global_default(subscriber)?;
167168
}
168169
Err(_) => {
169-
tracing::subscriber::set_global_default(subscriber)?;
170+
tracing::subscriber::set_global_default(subscriber.with(layer).with(filter))?;
170171
}
171172
};
172173

0 commit comments

Comments
 (0)