Skip to content

Commit 5dc2b15

Browse files
emschwartzhawkw
authored andcommitted
Fix global LogTracer installation by using InitError enum instead of Boxed errors (#406)
## Motivation I thought the boxing errors solution implemented in #400 was equivalent to the enum solution in emschwartz@f22bae5, but the current master doesn't actually work to install the global `LogTracer`. I can't figure out why the current cargo features don't end up turning on [this line][1], but I confirmed with the log example in 4511325 that it's not currently working. [1]: https://github.com/tokio-rs/tracing/blob/master/tracing-subscriber/src/fmt/mod.rs#L452-L453 * subscriber: use InitError enum instead of Box the Boxing solution while using the feature tracing-log/std didn't actually seem to work * examples: log compatibility * fix dependencies Signed-off-by: Eliza Weisman <[email protected]> * fix boxed errors Signed-off-by: Eliza Weisman <[email protected]> * cargo fmt Signed-off-by: Eliza Weisman <[email protected]>
1 parent a4bc8cb commit 5dc2b15

File tree

5 files changed

+23
-11
lines changed

5 files changed

+23
-11
lines changed

examples/examples/log.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fn main() {
2+
tracing_subscriber::fmt::Subscriber::builder()
3+
.with_max_level(tracing::Level::TRACE)
4+
.init();
5+
6+
log::debug!("this is a log line");
7+
tracing::debug!("this is a tracing line");
8+
}

tracing-log/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ readme = "README.md"
1818

1919
[features]
2020
default = ["log-tracer", "trace-logger", "std"]
21+
std = ["log/std"]
2122
log-tracer = []
2223
trace-logger = []
23-
std = ["log/std"]
2424

2525
[dependencies]
2626
tracing-core = "0.1.2"
27-
log = { version = "0.4", features = ["std"] }
27+
log = { version = "0.4" }
2828
lazy_static = "1.3.0"
2929
env_logger = { version = "0.6", optional = true }
3030

tracing-log/src/log_tracer.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
//! [ignore]: struct.Builder.html#method.ignore_crate
2929
use crate::{format_trace, AsTrace};
3030
use log;
31+
pub use log::SetLoggerError;
3132
use tracing_core::dispatcher;
3233

3334
/// A simple "logger" that converts all log records into `tracing` `Event`s.
@@ -110,7 +111,8 @@ impl LogTracer {
110111
/// initializing it.
111112
///
112113
/// [`builder`]: #method.builder
113-
pub fn init_with_filter(level: log::LevelFilter) -> Result<(), log::SetLoggerError> {
114+
#[cfg(feature = "std")]
115+
pub fn init_with_filter(level: log::LevelFilter) -> Result<(), SetLoggerError> {
114116
Self::builder().with_max_level(level).init()
115117
}
116118

@@ -143,7 +145,8 @@ impl LogTracer {
143145
///
144146
/// [`init_with_filter`]: #method.init_with_filter
145147
/// [`builder`]: #method.builder
146-
pub fn init() -> Result<(), log::SetLoggerError> {
148+
#[cfg(feature = "std")]
149+
pub fn init() -> Result<(), SetLoggerError> {
147150
Self::builder().init()
148151
}
149152
}
@@ -237,7 +240,8 @@ impl Builder {
237240
/// as the default logger.
238241
///
239242
/// Setting a global logger can only be done once.
240-
pub fn init(self) -> Result<(), log::SetLoggerError> {
243+
#[cfg(feature = "std")]
244+
pub fn init(self) -> Result<(), SetLoggerError> {
241245
let ignore_crates = self.ignore_crates.into_boxed_slice();
242246
let logger = Box::new(LogTracer { ignore_crates });
243247
log::set_boxed_logger(logger)?;

tracing-subscriber/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ smallvec = { optional = true, version = "0.6.10"}
3939
lazy_static = { optional = true, version = "1" }
4040

4141
# fmt
42-
tracing-log = { path = "../tracing-log", version = "0.1", optional = true, default-features = false, features = ["std", "log-tracer"] }
42+
tracing-log = { path = "../tracing-log", version = "0.1", optional = true, default-features = false, features = ["log-tracer", "std"] }
4343
ansi_term = { version = "0.11", optional = true }
4444
owning_ref = { version = "0.4.0", optional = true }
4545
chrono = { version = "0.4", optional = true }

tracing-subscriber/src/fmt/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -448,14 +448,14 @@ where
448448
/// Returns an Error if the initialization was unsuccessful, likely
449449
/// because a global subscriber was already installed by another
450450
/// call to `try_init`.
451-
pub fn try_init(self) -> Result<(), impl Error + Send + Sync + 'static> {
452-
#[cfg(feature = "tracing-log/std")]
453-
tracing_log::LogTracer::init().map_err(Box::new)?;
451+
pub fn try_init(self) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
452+
#[cfg(feature = "tracing-log")]
453+
tracing_log::LogTracer::init()?;
454454

455455
tracing_core::dispatcher::set_global_default(tracing_core::dispatcher::Dispatch::new(
456456
self.finish(),
457457
))
458-
.map_err(Box::new)
458+
.map_err(Into::into)
459459
}
460460

461461
/// Install this Subscriber as the global default.
@@ -859,7 +859,7 @@ impl Default for Settings {
859859
/// https://docs.rs/tracing-log/0.1.0/tracing_log/struct.LogTracer.html
860860
/// [`RUST_LOG` environment variable]:
861861
/// ../filter/struct.EnvFilter.html#associatedconstant.DEFAULT_ENV
862-
pub fn try_init() -> Result<(), impl Error + Send + Sync + 'static> {
862+
pub fn try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
863863
Subscriber::builder()
864864
.with_env_filter(crate::EnvFilter::from_default_env())
865865
.try_init()

0 commit comments

Comments
 (0)