better filtering logic

master
Ondřej Hruška 5 years ago
parent e1e5f2c2be
commit e4d855fecc
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 27
      src/lib.rs

@ -11,7 +11,7 @@ use std::io::Read;
use std::path::{Path, PathBuf};
use std::str::FromStr;
pub const LOG_LEVELS: [&str; 5] = ["error", "warn", "info", "debug", "trace"];
pub const LOG_LEVELS: [&str; 6] = ["off", "error", "warn", "info", "debug", "trace"];
/// Implement this for the main config struct
pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default {
@ -163,6 +163,13 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default {
println!();
builder.format_timestamp_millis();
let mut per_mod = vec![];
if let Some(mod_levels) = config.logging_mod_levels() {
for (module, lvl) in mod_levels {
per_mod.push((module, log::LevelFilter::from_str(lvl)?));
}
}
if let Some(suppress_mods) = config.logging_suppress_mods() {
// set logging level for suppressed libs
@ -173,16 +180,20 @@ pub trait AppConfig: Sized + Serialize + DeserializeOwned + Debug + Default {
sup_lvl = log::LevelFilter::Error;
}
for lib in suppress_mods {
'sup: for lib in suppress_mods {
for (module, _lvl) in per_mod.iter() {
if lib.starts_with(module.as_str()) {
// avoid suppressing if user set different level for a parent module
continue 'sup;
}
}
builder.filter_module(lib, sup_lvl);
}
}
if let Some(mod_levels) = config.logging_mod_levels() {
for (module, lvl) in mod_levels {
let lvl = log::LevelFilter::from_str(lvl)?;
builder.filter_module(module, lvl);
}
for (module, lvl) in per_mod.into_iter() {
builder.filter_module(module, lvl);
}
builder.init();

Loading…
Cancel
Save