diff --git a/elfo-logger/src/filtering_layer.rs b/elfo-logger/src/filtering_layer.rs index e70051c3..9d7d73b6 100644 --- a/elfo-logger/src/filtering_layer.rs +++ b/elfo-logger/src/filtering_layer.rs @@ -2,6 +2,7 @@ use std::sync::Arc; use arc_swap::ArcSwap; use fxhash::FxHashMap; +#[cfg(feature = "tracing-log")] use once_cell::sync::OnceCell; use tracing::{metadata::LevelFilter, subscriber::Interest, Metadata, Subscriber}; use tracing_subscriber::{ @@ -28,6 +29,7 @@ impl Default for FilteringConfig { struct Inner { config: ArcSwap, + #[cfg(feature = "tracing-log")] log_metadata_name: OnceCell<&'static str>, } @@ -41,6 +43,7 @@ impl FilteringLayer { Self { inner: Arc::new(Inner { config: ArcSwap::new(Arc::new(FilteringConfig::default())), + #[cfg(feature = "tracing-log")] log_metadata_name: OnceCell::new(), }), } @@ -80,28 +83,31 @@ impl Layer for FilteringLayer { // would already eliminate logs that would be filtered by it. let level = *meta.level(); - // `tracing-log` doesn't call `.register_callsite()`, so we have to recheck - // `.targets` in this one case. - let name = meta.name(); - if let Some(&log_name) = self.inner.log_metadata_name.get() { - // We've already got logs from `tracing-log`, just compare str - // pointers for performance. - if std::ptr::eq(log_name, name) { + #[cfg(feature = "tracing-log")] + { + // `tracing-log` doesn't call `.register_callsite()`, so we have to recheck + // `.targets` in this one case. + let name = meta.name(); + if let Some(&log_name) = self.inner.log_metadata_name.get() { + // We've already got logs from `tracing-log`, just compare str + // pointers for performance. + if std::ptr::eq(log_name, name) { + let config = self.inner.config.load(); + if !config.targets.would_enable(meta.target(), meta.level()) { + return false; + } + } + } else if name == "log record" { let config = self.inner.config.load(); if !config.targets.would_enable(meta.target(), meta.level()) { return false; } - } - } else if name == "log record" { - let config = self.inner.config.load(); - if !config.targets.would_enable(meta.target(), meta.level()) { - return false; - } - // That's "initialized tracing_log adapter" to set `log_metadata_name`, just - // ignore it. Any actual logs from `elfo_logger` would use `tracing`. - if meta.target() == "elfo_logger" { - self.inner.log_metadata_name.set(name).ok(); - return false; + // That's "initialized tracing_log adapter" to set `log_metadata_name`, just + // ignore it. Any actual logs from `elfo_logger` would use `tracing`. + if meta.target() == "elfo_logger" { + self.inner.log_metadata_name.set(name).ok(); + return false; + } } }