Skip to content

Commit e61ebff

Browse files
committed
subscriber: Use Targets for the default subscriber if env-filter is not enabled
Close #1697
1 parent 2da6cc5 commit e61ebff

File tree

1 file changed

+19
-1
lines changed
  • tracing-subscriber/src/fmt

1 file changed

+19
-1
lines changed

tracing-subscriber/src/fmt/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@
297297
//! https://docs.rs/tracing/latest/tracing/trait.Subscriber.html
298298
//! [`tracing`]: https://crates.io/crates/tracing
299299
//! [`fmt::format`]: mod@crate::fmt::format
300+
#[cfg(not(feature = "env-filter"))]
301+
use core::str::FromStr;
300302
use std::{any::TypeId, error::Error, io};
301303
use tracing_core::{span, subscriber::Interest, Event, Metadata};
302304

@@ -316,6 +318,7 @@ use crate::{
316318
layer,
317319
registry::{LookupSpan, Registry},
318320
};
321+
use crate::util::SubscriberInitExt;
319322

320323
#[doc(inline)]
321324
pub use self::{
@@ -1131,7 +1134,22 @@ pub fn try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
11311134
#[cfg(feature = "env-filter")]
11321135
let builder = builder.with_env_filter(crate::EnvFilter::from_default_env());
11331136

1134-
builder.try_init()
1137+
let subscriber = builder.finish();
1138+
#[cfg(not(feature = "env-filter"))]
1139+
let targets = match std::env::var("RUST_LOG") {
1140+
Ok(var) => {
1141+
crate::filter::Targets::from_str(&var).map_err(|e|{eprintln!("Ignoring `RUST_LOG`: {:?}", e);}).unwrap_or_default()
1142+
}
1143+
Err(std::env::VarError::NotPresent) => {crate::filter::Targets::new()}
1144+
Err(e) => {
1145+
eprintln!("Ignoring `RUST_LOG`: {:?}", e);
1146+
crate::filter::Targets::new()
1147+
}
1148+
}.with_default(crate::fmt::Subscriber::DEFAULT_MAX_LEVEL);
1149+
#[cfg(not(feature = "env-filter"))]
1150+
let subscriber = subscriber.with(targets);
1151+
1152+
subscriber.try_init().map_err(Into::into)
11351153
}
11361154

11371155
/// Install a global tracing subscriber that listens for events and

0 commit comments

Comments
 (0)