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

Add methods for filtering by module and level #84

Merged
merged 2 commits into from
Apr 21, 2018
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
12 changes: 11 additions & 1 deletion src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,16 @@ impl Builder {
builder
}

/// Adds a directive to the filter for a specific module.
pub fn filter_module(&mut self, module: &str, level: LevelFilter) -> &mut Self {
self.filter(Some(module), level)
}

/// Adds a directive to the filter for all modules.
pub fn filter_level(&mut self, level: LevelFilter) -> &mut Self {
self.filter(None, level)
}

/// Adds a directive to the filter.
///
/// The given module (if any) will log at most the specified level provided.
Expand All @@ -206,7 +216,7 @@ impl Builder {
level: LevelFilter) -> &mut Self {
self.directives.push(Directive {
name: module.map(|s| s.to_string()),
level: level,
level,
});
self
}
Expand Down
8 changes: 4 additions & 4 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl Style {
/// });
/// ```
pub fn set_color(&mut self, color: Color) -> &mut Style {
self.spec.set_fg(color.to_termcolor());
self.spec.set_fg(color.into_termcolor());
self
}

Expand Down Expand Up @@ -366,7 +366,7 @@ impl Style {
/// });
/// ```
pub fn set_bg(&mut self, color: Color) -> &mut Style {
self.spec.set_bg(color.to_termcolor());
self.spec.set_bg(color.into_termcolor());
self
}

Expand Down Expand Up @@ -671,7 +671,7 @@ impl fmt::Display for ParseColorError {
}

impl Color {
fn to_termcolor(self) -> Option<termcolor::Color> {
fn into_termcolor(self) -> Option<termcolor::Color> {
match self {
Color::Black => Some(termcolor::Color::Black),
Color::Blue => Some(termcolor::Color::Blue),
Expand Down Expand Up @@ -709,7 +709,7 @@ impl FromStr for Color {

fn from_str(s: &str) -> Result<Color, ParseColorError> {
let tc = termcolor::Color::from_str(s).map_err(ParseColorError::termcolor)?;
Color::from_termcolor(tc).ok_or_else(|| ParseColorError::unrecognized(s.to_owned()))
Color::from_termcolor(tc).ok_or_else(|| ParseColorError::unrecognized(s.into()))
}
}

Expand Down
46 changes: 46 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,52 @@ impl Builder {
self
}

/// Adds a directive to the filter for a specific module.
///
/// # Examples
///
/// Only include messages for warning and above for logs in `path::to::module`:
///
/// ```
/// # extern crate log;
/// # extern crate env_logger;
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::Builder;
///
/// let mut builder = Builder::new();
///
/// builder.filter_module("path::to::module", LevelFilter::Info);
/// # }
/// ```
pub fn filter_module(&mut self, module: &str, level: LevelFilter) -> &mut Self {
self.filter.filter_module(module, level);
self
}

/// Adds a directive to the filter for all modules.
///
/// # Examples
///
/// Only include messages for warning and above for logs in `path::to::module`:
///
/// ```
/// # extern crate log;
/// # extern crate env_logger;
/// # fn main() {
/// use log::LevelFilter;
/// use env_logger::Builder;
///
/// let mut builder = Builder::new();
///
/// builder.filter_level(LevelFilter::Info);
/// # }
/// ```
pub fn filter_level(&mut self, level: LevelFilter) -> &mut Self {
self.filter.filter_level(level);
self
}

/// Adds filters to the logger.
///
/// The given module (if any) will log at most the specified level provided.
Expand Down