Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subscriber/fmt: address UX issues in defaults #1781

Merged
merged 6 commits into from
Dec 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion tracing-subscriber/src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ pub mod writer;
pub use fmt_layer::{FmtContext, FormattedFields, Layer};

use crate::layer::Layer as _;
use crate::util::SubscriberInitExt;
use crate::{
filter::LevelFilter,
layer,
Expand Down Expand Up @@ -1131,7 +1132,37 @@ pub fn try_init() -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
#[cfg(feature = "env-filter")]
let builder = builder.with_env_filter(crate::EnvFilter::from_default_env());

builder.try_init()
// If `env-filter` is disabled, remove the default max level filter from the
// subscriber; it will be added to the `Targets` filter instead if no filter
// is set in `RUST_LOG`.
// Replacing the default `LevelFilter` with an `EnvFilter` would imply this,
// but we can't replace the builder's filter with a `Targets` filter yet.
#[cfg(not(feature = "env-filter"))]
let builder = builder.with_max_level(LevelFilter::TRACE);

let subscriber = builder.finish();
#[cfg(not(feature = "env-filter"))]
let subscriber = {
use crate::{filter::Targets, layer::SubscriberExt};
use std::{env, str::FromStr};
let targets = match env::var("RUST_LOG") {
Ok(var) => Targets::from_str(&var)
.map_err(|e| {
eprintln!("Ignoring `RUST_LOG={:?}`: {}", var, e);
})
.unwrap_or_default(),
Err(env::VarError::NotPresent) => {
Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
}
Err(e) => {
eprintln!("Ignoring `RUST_LOG`: {}", e);
Targets::new().with_default(Subscriber::DEFAULT_MAX_LEVEL)
}
};
subscriber.with(targets)
};

subscriber.try_init().map_err(Into::into)
}

/// Install a global tracing subscriber that listens for events and
Expand Down