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 subscriber, we can use the subscriber'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 `FmtSubscriber`'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 committed Feb 18, 2021
1 parent 9d31561 commit be935d7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 10 deletions.
2 changes: 1 addition & 1 deletion tracing-log/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tracing-log"
version = "0.1.1"
version = "0.1.2"
authors = ["Tokio Contributors <team@tokio.rs>"]
edition = "2018"
repository = "https://github.com/tokio-rs/tracing"
Expand Down
33 changes: 33 additions & 0 deletions tracing-log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,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
2 changes: 1 addition & 1 deletion tracing-subscriber/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ thread_local = { version = "1.0.1", optional = true }
[dev-dependencies]
tracing = { path = "../tracing", version = "0.1" }
log = "0.4"
tracing-log = { path = "../tracing-log", version = "0.1" }
tracing-log = { path = "../tracing-log", version = "0.1.2" }
criterion = { version = "0.3", default_features = false }
regex = { version = "1", default-features = false, features = ["std"] }
tracing-futures = { path = "../tracing-futures", version = "0.2", default-features = false, features = ["std-future", "std"] }
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 @@ -547,12 +547,9 @@ where
/// because a global subscriber 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::dispatcher::set_global_default(tracing_core::dispatcher::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::dispatcher::{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]: https://docs.rs/tracing/0.1.21/tracing/dispatcher/index.html#setting-the-default-subscriber
/// [`log`]: https://crates.io/log
fn try_init(self) -> Result<(), TryInitError> {
#[cfg(feature = "tracing-log")]
tracing_log::LogTracer::init().map_err(TryInitError::new)?;

dispatcher::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 be935d7

Please sign in to comment.