Replace a command line flag with an env var to allow tools to initialize the tracing loggers at their own discretion
This commit is contained in:
parent
dffea43fc1
commit
f95b553eb4
6 changed files with 8 additions and 37 deletions
|
@ -54,25 +54,12 @@ use tracing_subscriber::fmt::{
|
|||
use tracing_subscriber::layer::SubscriberExt;
|
||||
|
||||
pub fn init_rustc_env_logger() -> Result<(), Error> {
|
||||
init_rustc_env_logger_with_backtrace_option(&None)
|
||||
}
|
||||
|
||||
pub fn init_rustc_env_logger_with_backtrace_option(
|
||||
backtrace_target: &Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
init_env_logger_with_backtrace_option("RUSTC_LOG", backtrace_target)
|
||||
init_env_logger("RUSTC_LOG")
|
||||
}
|
||||
|
||||
/// In contrast to `init_rustc_env_logger` this allows you to choose an env var
|
||||
/// other than `RUSTC_LOG`.
|
||||
pub fn init_env_logger(env: &str) -> Result<(), Error> {
|
||||
init_env_logger_with_backtrace_option(env, &None)
|
||||
}
|
||||
|
||||
pub fn init_env_logger_with_backtrace_option(
|
||||
env: &str,
|
||||
backtrace_target: &Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
let filter = match env::var(env) {
|
||||
Ok(env) => EnvFilter::new(env),
|
||||
_ => EnvFilter::default().add_directive(Directive::from(LevelFilter::WARN)),
|
||||
|
@ -106,8 +93,8 @@ pub fn init_env_logger_with_backtrace_option(
|
|||
let layer = layer.with_thread_ids(true).with_thread_names(true);
|
||||
|
||||
let subscriber = tracing_subscriber::Registry::default().with(filter).with(layer);
|
||||
match backtrace_target {
|
||||
Some(str) => {
|
||||
match env::var(format!("{env}_BACKTRACE")) {
|
||||
Ok(str) => {
|
||||
let fmt_layer = tracing_subscriber::fmt::layer()
|
||||
.with_writer(io::stderr)
|
||||
.without_time()
|
||||
|
@ -115,7 +102,7 @@ pub fn init_env_logger_with_backtrace_option(
|
|||
let subscriber = subscriber.with(fmt_layer);
|
||||
tracing::subscriber::set_global_default(subscriber).unwrap();
|
||||
}
|
||||
None => {
|
||||
Err(_) => {
|
||||
tracing::subscriber::set_global_default(subscriber).unwrap();
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue