Skip to content

Commit

Permalink
Merge pull request paritytech#26 from gear-tech/cl/fix-logging
Browse files Browse the repository at this point in the history
chore(sc-tracing): fix the max_level implementation of Subscriber
  • Loading branch information
clearloop authored and ukint-vs committed Apr 13, 2023
1 parent 6fec3a9 commit 53066e8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
14 changes: 14 additions & 0 deletions client/tracing/src/logging/directives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ static DEFAULT_DIRECTIVES: OnceCell<Mutex<Vec<String>>> = OnceCell::new();
// Current state of log filter
static CURRENT_DIRECTIVES: OnceCell<Mutex<Vec<String>>> = 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::<Vec<&str>>().join(",")
}

/// Add log filter directive(s) to the defaults
///
/// The syntax is identical to the CLI `<target>=<level>`:
Expand Down
29 changes: 10 additions & 19 deletions client/tracing/src/logging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,6 @@ fn to_log_level_filter(level_filter: Option<LevelFilter>) -> log::LevelFilter {
}
}

/// Convert a [`log::LevelFilter`] to a `Option<LevelFilter>`.
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<N, E, F, W>(
directives: &str,
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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);
Expand All @@ -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)
Expand Down

0 comments on commit 53066e8

Please sign in to comment.