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

Added support for module level filtering #89

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
42 changes: 36 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,48 @@ impl SimpleLogger {
/// [`with_level`] if `RUST_LOG` is not set or can't be parsed as a
/// standard log level.
///
/// module/target level filtering is supported
///
/// the following format is expected:
/// ```text
/// RUST_LOG=[default-level][,][target][=][level][,...]
/// ```
/// example:
/// ```text
/// RUST_LOG=warn,data=debug,hardware=debug
/// ```
///
/// This must be called after [`with_level`]. If called before
/// [`with_level`], it will have no effect.
///
/// [`with_level`]: #method.with_level
#[must_use = "You must call init() to begin logging"]
pub fn env(mut self) -> SimpleLogger {
self.default_level = std::env::var("RUST_LOG")
.ok()
.as_deref()
.map(log::LevelFilter::from_str)
.and_then(Result::ok)
.unwrap_or(self.default_level);
if let Ok(modules) = std::env::var("RUST_LOG").as_deref() {
let mut iter = modules.split(',');

if let Some((module, level)) = iter
.next()
.map(|pair| match log::LevelFilter::from_str(pair) {
Ok(level) => {
self.default_level = level;
None
}
Err(_) => Some(pair),
})
.and_then(|x| x)
.map(|pair| pair.split_once('='))
.and_then(|x| x)
{
let level = log::LevelFilter::from_str(level).unwrap_or(self.default_level);
self.module_levels.push((module.to_string(), level));
}

for (module, level) in iter.flat_map(|x| x.split_once('=')) {
let level = log::LevelFilter::from_str(level).unwrap_or(self.default_level);
self.module_levels.push((module.to_string(), level));
}
}

self
}
Expand Down
Loading