Skip to content

Commit be935d7

Browse files
committed
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>
1 parent 9d31561 commit be935d7

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

tracing-log/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tracing-log"
3-
version = "0.1.1"
3+
version = "0.1.2"
44
authors = ["Tokio Contributors <team@tokio.rs>"]
55
edition = "2018"
66
repository = "https://github.com/tokio-rs/tracing"

tracing-log/src/lib.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,39 @@ impl AsTrace for log::Level {
380380
}
381381
}
382382

383+
impl crate::sealed::Sealed for log::LevelFilter {}
384+
385+
impl AsTrace for log::LevelFilter {
386+
type Trace = tracing_core::LevelFilter;
387+
#[inline]
388+
fn as_trace(&self) -> tracing_core::LevelFilter {
389+
match self {
390+
log::LevelFilter::Off => tracing_core::LevelFilter::OFF,
391+
log::LevelFilter::Error => tracing_core::LevelFilter::ERROR,
392+
log::LevelFilter::Warn => tracing_core::LevelFilter::WARN,
393+
log::LevelFilter::Info => tracing_core::LevelFilter::INFO,
394+
log::LevelFilter::Debug => tracing_core::LevelFilter::DEBUG,
395+
log::LevelFilter::Trace => tracing_core::LevelFilter::TRACE,
396+
}
397+
}
398+
}
399+
400+
impl crate::sealed::Sealed for tracing_core::LevelFilter {}
401+
402+
impl AsLog for tracing_core::LevelFilter {
403+
type Log = log::LevelFilter;
404+
#[inline]
405+
fn as_log(&self) -> Self::Log {
406+
match *self {
407+
tracing_core::LevelFilter::OFF => log::LevelFilter::Off,
408+
tracing_core::LevelFilter::ERROR => log::LevelFilter::Error,
409+
tracing_core::LevelFilter::WARN => log::LevelFilter::Warn,
410+
tracing_core::LevelFilter::INFO => log::LevelFilter::Info,
411+
tracing_core::LevelFilter::DEBUG => log::LevelFilter::Debug,
412+
tracing_core::LevelFilter::TRACE => log::LevelFilter::Trace,
413+
}
414+
}
415+
}
383416
/// Extends log `Event`s to provide complete `Metadata`.
384417
///
385418
/// In `tracing-log`, an `Event` produced by a log (through [`AsTrace`]) has an hard coded

tracing-subscriber/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ thread_local = { version = "1.0.1", optional = true }
6060
[dev-dependencies]
6161
tracing = { path = "../tracing", version = "0.1" }
6262
log = "0.4"
63-
tracing-log = { path = "../tracing-log", version = "0.1" }
63+
tracing-log = { path = "../tracing-log", version = "0.1.2" }
6464
criterion = { version = "0.3", default_features = false }
6565
regex = { version = "1", default-features = false, features = ["std"] }
6666
tracing-futures = { path = "../tracing-futures", version = "0.2", default-features = false, features = ["std-future", "std"] }

tracing-subscriber/src/fmt/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -547,12 +547,9 @@ where
547547
/// because a global subscriber was already installed by another
548548
/// call to `try_init`.
549549
pub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
550-
#[cfg(feature = "tracing-log")]
551-
tracing_log::LogTracer::init().map_err(Box::new)?;
550+
use crate::util::SubscriberInitExt;
551+
self.finish().try_init()?;
552552

553-
tracing_core::dispatcher::set_global_default(tracing_core::dispatcher::Dispatch::new(
554-
self.finish(),
555-
))?;
556553
Ok(())
557554
}
558555

tracing-subscriber/src/util.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
//! ergonomic.
33
use std::{error::Error, fmt};
44
use tracing_core::dispatcher::{self, Dispatch};
5+
#[cfg(feature = "tracing-log")]
6+
use tracing_log::AsLog;
57

68
/// Extension trait adding utility methods for subscriber initialization.
79
///
@@ -50,11 +52,20 @@ where
5052
/// [global default subscriber]: https://docs.rs/tracing/0.1.21/tracing/dispatcher/index.html#setting-the-default-subscriber
5153
/// [`log`]: https://crates.io/log
5254
fn try_init(self) -> Result<(), TryInitError> {
53-
#[cfg(feature = "tracing-log")]
54-
tracing_log::LogTracer::init().map_err(TryInitError::new)?;
55-
5655
dispatcher::set_global_default(self.into()).map_err(TryInitError::new)?;
5756

57+
// Since we are setting the global default subscriber, we can
58+
// opportunistically go ahead and set its global max level hint as
59+
// the max level for the `log` crate as well. This should make
60+
// skipping `log` diagnostics much faster.
61+
#[cfg(feature = "tracing-log")]
62+
tracing_log::LogTracer::builder()
63+
// Note that we must call this *after* setting the global default
64+
// subscriber, so that we get its max level hint.
65+
.with_max_level(tracing_core::LevelFilter::current().as_log())
66+
.init()
67+
.map_err(TryInitError::new)?;
68+
5869
Ok(())
5970
}
6071

0 commit comments

Comments
 (0)