Skip to content

Commit

Permalink
subscriber: set the max log LevelFilter in init (#1248)
Browse files Browse the repository at this point in the history
Depends on #1247.

Since `tracing-subscriber`'s `init` and `try_init` functions set the
global default collector, we can use the collector's max-level hint as
the max level for the log crate, as well. This should significantly
improve performance for skipping `log` records that fall below the
collector's max level, as they will not have to call the
`LogTracer::enabled` method.

This will prevent issues like bytecodealliance/wasmtime#2662
from occurring in the future. See also #1249.

In order to implement this, I also changed the `FmtCollector`'s
`try_init` to just use `util::SubscriberInitExt`'s `try_init` function,
so that the same code isn't duplicated in multiple places. I also
added `AsLog` and `AsTrace` conversions for `LevelFilter`s in
the `tracing-log` crate.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
  • Loading branch information
hawkw authored Feb 18, 2021
1 parent cc525a6 commit 002f0bb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
33 changes: 33 additions & 0 deletions tracing-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,39 @@ impl AsTrace for log::Level {
}
}

impl crate::sealed::Sealed for log::LevelFilter {}

impl AsTrace for log::LevelFilter {
type Trace = tracing_core::LevelFilter;
#[inline]
fn as_trace(&self) -> tracing_core::LevelFilter {
match self {
log::LevelFilter::Off => tracing_core::LevelFilter::OFF,
log::LevelFilter::Error => tracing_core::LevelFilter::ERROR,
log::LevelFilter::Warn => tracing_core::LevelFilter::WARN,
log::LevelFilter::Info => tracing_core::LevelFilter::INFO,
log::LevelFilter::Debug => tracing_core::LevelFilter::DEBUG,
log::LevelFilter::Trace => tracing_core::LevelFilter::TRACE,
}
}
}

impl crate::sealed::Sealed for tracing_core::LevelFilter {}

impl AsLog for tracing_core::LevelFilter {
type Log = log::LevelFilter;
#[inline]
fn as_log(&self) -> Self::Log {
match *self {
tracing_core::LevelFilter::OFF => log::LevelFilter::Off,
tracing_core::LevelFilter::ERROR => log::LevelFilter::Error,
tracing_core::LevelFilter::WARN => log::LevelFilter::Warn,
tracing_core::LevelFilter::INFO => log::LevelFilter::Info,
tracing_core::LevelFilter::DEBUG => log::LevelFilter::Debug,
tracing_core::LevelFilter::TRACE => log::LevelFilter::Trace,
}
}
}
/// Extends log `Event`s to provide complete `Metadata`.
///
/// In `tracing-log`, an `Event` produced by a log (through [`AsTrace`]) has an hard coded
Expand Down
7 changes: 2 additions & 5 deletions tracing-subscriber/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -566,12 +566,9 @@ where
/// because a global collector was already installed by another
/// call to `try_init`.
pub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
#[cfg(feature = "tracing-log")]
tracing_log::LogTracer::init().map_err(Box::new)?;
use crate::util::SubscriberInitExt;
self.finish().try_init()?;

tracing_core::dispatch::set_global_default(tracing_core::dispatch::Dispatch::new(
self.finish(),
))?;
Ok(())
}

Expand Down
17 changes: 14 additions & 3 deletions tracing-subscriber/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
//! ergonomic.
use std::{error::Error, fmt};
use tracing_core::dispatch::{self, Dispatch};
#[cfg(feature = "tracing-log")]
use tracing_log::AsLog;

/// Extension trait adding utility methods for subscriber initialization.
///
Expand Down Expand Up @@ -50,11 +52,20 @@ where
/// [global default subscriber]: tracing::dispatch#setting-the-default-collector
/// [`log`]: https://crates.io/log
fn try_init(self) -> Result<(), TryInitError> {
#[cfg(feature = "tracing-log")]
tracing_log::LogTracer::init().map_err(TryInitError::new)?;

dispatch::set_global_default(self.into()).map_err(TryInitError::new)?;

// Since we are setting the global default subscriber, we can
// opportunistically go ahead and set its global max level hint as
// the max level for the `log` crate as well. This should make
// skipping `log` diagnostics much faster.
#[cfg(feature = "tracing-log")]
tracing_log::LogTracer::builder()
// Note that we must call this *after* setting the global default
// subscriber, so that we get its max level hint.
.with_max_level(tracing_core::LevelFilter::current().as_log())
.init()
.map_err(TryInitError::new)?;

Ok(())
}

Expand Down

0 comments on commit 002f0bb

Please sign in to comment.