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

chore(sc-tracing): fix the max_level implementation of Subscriber #26

Merged
merged 4 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
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
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