Skip to content

Commit

Permalink
fix the max_level implementation of Subscriber
Browse files Browse the repository at this point in the history
prs #24 , #26
  • Loading branch information
gshep committed Apr 1, 2023
1 parent 6b1d7e8 commit e5660d8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 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
30 changes: 24 additions & 6 deletions client/tracing/src/logging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ fn prepare_subscriber<N, E, F, W>(
profiling_targets: Option<&str>,
force_colors: Option<bool>,
detailed_output: bool,
max_level_override: Option<log::LevelFilter>,
builder_hook: impl Fn(
SubscriberBuilder<format::DefaultFields, EventFormat, EnvFilter, DefaultLogger>,
) -> SubscriberBuilder<N, E, F, W>,
Expand Down Expand Up @@ -142,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 All @@ -160,7 +165,7 @@ where
}

let max_level_hint = Layer::<FmtSubscriber>::max_level_hint(&env_filter);
let max_level = to_log_level_filter(max_level_hint);
let max_level = max_level_override.unwrap_or(to_log_level_filter(max_level_hint));

tracing_log::LogTracer::builder().with_max_level(max_level).init()?;

Expand All @@ -181,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,6 +210,7 @@ pub struct LoggerBuilder {
log_reloading: bool,
force_colors: Option<bool>,
detailed_output: bool,
max_level: Option<log::LevelFilter>,
}

impl LoggerBuilder {
Expand All @@ -216,6 +223,7 @@ impl LoggerBuilder {
log_reloading: false,
force_colors: None,
detailed_output: false,
max_level: None,
}
}

Expand Down Expand Up @@ -261,6 +269,12 @@ impl LoggerBuilder {
self
}

/// Override log level.
pub fn with_max_level(&mut self, level: log::LevelFilter) -> &mut Self {
self.max_level = Some(level);
self
}

/// Initialize the global logger
///
/// This sets various global logging and tracing instances and thus may only be called once.
Expand All @@ -272,6 +286,7 @@ impl LoggerBuilder {
Some(&profiling_targets),
self.force_colors,
self.detailed_output,
self.max_level,
|builder| enable_log_reloading!(builder),
)?;
let mut profiling =
Expand All @@ -290,6 +305,7 @@ impl LoggerBuilder {
Some(&profiling_targets),
self.force_colors,
self.detailed_output,
self.max_level,
|builder| builder,
)?;
let mut profiling =
Expand All @@ -309,6 +325,7 @@ impl LoggerBuilder {
None,
self.force_colors,
self.detailed_output,
self.max_level,
|builder| enable_log_reloading!(builder),
)?;

Expand All @@ -321,6 +338,7 @@ impl LoggerBuilder {
None,
self.force_colors,
self.detailed_output,
self.max_level,
|builder| builder,
)?;

Expand Down

0 comments on commit e5660d8

Please sign in to comment.