From e4d855fecc8cae5990aaadfd1a5e43f59a6dff4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Mon, 20 Apr 2020 09:40:58 +0200 Subject: [PATCH] better filtering logic --- src/lib.rs | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index e09be2e..675198c 100644 --- a/src/lib.rs +++ b/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();