mirror of
https://github.com/tokio-rs/tracing.git
synced 2025-09-29 22:10:38 +00:00
subscriber: set the max log
LevelFilter
in init
(#1248)
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>
This commit is contained in:
parent
2a9d17f73f
commit
31aa6afecc
@ -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"
|
||||
|
@ -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
|
||||
|
@ -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"] }
|
||||
|
@ -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(())
|
||||
}
|
||||
|
||||
|
@ -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.
|
||||
///
|
||||
@ -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(())
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user