From 53066e81f96fcfe1865b2f8728d74a365c4b0519 Mon Sep 17 00:00:00 2001 From: clearloop <26088946+clearloop@users.noreply.github.com> Date: Sun, 4 Dec 2022 00:35:21 +0800 Subject: [PATCH] Merge pull request #26 from gear-tech/cl/fix-logging chore(sc-tracing): fix the max_level implementation of Subscriber --- client/tracing/src/logging/directives.rs | 14 ++++++++++++ client/tracing/src/logging/mod.rs | 29 ++++++++---------------- 2 files changed, 24 insertions(+), 19 deletions(-) diff --git a/client/tracing/src/logging/directives.rs b/client/tracing/src/logging/directives.rs index 3985bf2d88c64..0ac03203dce99 100644 --- a/client/tracing/src/logging/directives.rs +++ b/client/tracing/src/logging/directives.rs @@ -27,6 +27,20 @@ static DEFAULT_DIRECTIVES: OnceCell>> = OnceCell::new(); // Current state of log filter static CURRENT_DIRECTIVES: OnceCell>> = OnceCell::new(); +/// Filiter directives with log level. +pub fn filter_directives(lvl: log::LevelFilter, dir: &str) -> String { + dir.split(",").filter(|dir|{ + match lvl { + log::LevelFilter::Off => false, + log::LevelFilter::Error => dir.contains("error"), + log::LevelFilter::Warn => dir.contains("error") || dir.contains("warn"), + log::LevelFilter::Info => dir.contains("error") || dir.contains("warn") || dir.contains("info"), + log::LevelFilter::Debug => dir.contains("error") || dir.contains("warn") || dir.contains("info") || dir.contains("debug"), + log::LevelFilter::Trace => true + } + }).collect::>().join(",") +} + /// Add log filter directive(s) to the defaults /// /// The syntax is identical to the CLI `=`: diff --git a/client/tracing/src/logging/mod.rs b/client/tracing/src/logging/mod.rs index d705982ef7390..8fc638d677600 100644 --- a/client/tracing/src/logging/mod.rs +++ b/client/tracing/src/logging/mod.rs @@ -90,18 +90,6 @@ fn to_log_level_filter(level_filter: Option) -> log::LevelFilter { } } -/// Convert a [`log::LevelFilter`] to a `Option`. -fn from_log_level_filter(level: log::LevelFilter) -> LevelFilter { - match level { - log::LevelFilter::Info => LevelFilter::INFO, - log::LevelFilter::Trace => LevelFilter::TRACE, - log::LevelFilter::Warn => LevelFilter::WARN, - log::LevelFilter::Error => LevelFilter::ERROR, - log::LevelFilter::Debug => LevelFilter::DEBUG, - log::LevelFilter::Off => LevelFilter::OFF, - } -} - /// Common implementation to get the subscriber. fn prepare_subscriber( directives: &str, @@ -155,11 +143,15 @@ where .expect("provided directive is valid"), ); - if let Ok(lvl) = std::env::var("RUST_LOG") { - if lvl != "" { - env_filter = parse_user_directives(env_filter, &lvl)?; - } - } + if let Ok(mut lvl) = std::env::var("RUST_LOG") { + if let Some(max_level) = max_level_override { + lvl = filter_directives(max_level, &lvl) + } + + if lvl != "" { + env_filter = parse_user_directives(env_filter, &lvl)?; + } + } if directives != "" { env_filter = parse_user_directives(env_filter, directives)?; @@ -194,6 +186,7 @@ where enable_color, dup_to_stdout: !atty::is(atty::Stream::Stderr) && atty::is(atty::Stream::Stdout), }; + let builder = FmtSubscriber::builder().with_env_filter(env_filter); let builder = builder.with_span_events(format::FmtSpan::NONE); @@ -204,8 +197,6 @@ where let builder = builder_hook(builder); - let builder = builder.with_max_level(from_log_level_filter(max_level)); - let subscriber = builder.finish().with(PrefixLayer); Ok(subscriber)