Skip to content

Commit 2a9d17f

Browse files
committed
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 d173c2d commit 2a9d17f

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
@@ -368,6 +368,7 @@ impl crate::sealed::Sealed for log::Level {}
368368

369369
impl AsTrace for log::Level {
370370
type Trace = tracing_core::Level;
371+
#[inline]
371372
fn as_trace(&self) -> tracing_core::Level {
372373
match self {
373374
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
@@ -160,7 +160,17 @@ impl Default for LogTracer {
160160

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

0 commit comments

Comments
 (0)