Skip to content

Commit 21dfa9d

Browse files
authored
log: compare log record Levels against the max level (#1247)
This branch adds a check against the current value of the max global `LevelFilter` hint in `tracing_log`'s `LogTracer::enabled` method. This should prevent `log::${LEVEL}_enabled!` from always returning `true` when `tracing` is in use, fixing a performance issue when consuming `log` records from tracing (see bytecodealliance/wasmtime#2662). This does, however, mean `LogTracer::enabled` will always be called if the max `log` level is not set. We can't determine whether we can set `log`'s max level in `tracing_log`, since we don't know if subscribers will change (resulting in the current max `tracing` level filter changing), or if the current subscriber is set as the global default. Higher-level code in `tracing_subscriber` can do this, though, in `init()`, because it _does_ know that it's setting a global default. I'll add that in a follow-up PR. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
1 parent 1b800fa commit 21dfa9d

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

tracing-log/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,7 @@ impl crate::sealed::Sealed for log::Level {}
346346

347347
impl AsTrace for log::Level {
348348
type Trace = tracing_core::Level;
349+
#[inline]
349350
fn as_trace(&self) -> tracing_core::Level {
350351
match self {
351352
log::Level::Error => tracing_core::Level::ERROR,

tracing-log/src/log_tracer.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,17 @@ impl Default for LogTracer {
158158

159159
impl log::Log for LogTracer {
160160
fn enabled(&self, metadata: &log::Metadata<'_>) -> bool {
161+
// First, check the log record against the current max level enabled by
162+
// the current `tracing` subscriber.
163+
if metadata.level().as_trace() > tracing_core::LevelFilter::current() {
164+
// If the log record's level is above that, disable it.
165+
return false;
166+
}
167+
168+
// Okay, it wasn't disabled by the max level — do we have any specific
169+
// modules to ignore?
161170
if self.ignore_crates.is_empty() {
171+
// If we don't, just enable it.
162172
return true;
163173
}
164174

0 commit comments

Comments
 (0)