From d6e969f8c2bd434950aa83bf91362195364cc8f5 Mon Sep 17 00:00:00 2001 From: Ashley Mannix Date: Sat, 21 Apr 2018 19:57:44 +1000 Subject: [PATCH] Add methods for filtering by module and level (#84) * add methods for filtering by module and level * misc internal cleanup --- src/filter/mod.rs | 12 +++++++++++- src/fmt.rs | 8 ++++---- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 5 deletions(-) diff --git a/src/filter/mod.rs b/src/filter/mod.rs index c3b67e8f..80a3f30f 100644 --- a/src/filter/mod.rs +++ b/src/filter/mod.rs @@ -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. @@ -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 } diff --git a/src/fmt.rs b/src/fmt.rs index 2d5806b6..76e46c7a 100644 --- a/src/fmt.rs +++ b/src/fmt.rs @@ -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 } @@ -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 } @@ -671,7 +671,7 @@ impl fmt::Display for ParseColorError { } impl Color { - fn to_termcolor(self) -> Option { + fn into_termcolor(self) -> Option { match self { Color::Black => Some(termcolor::Color::Black), Color::Blue => Some(termcolor::Color::Blue), @@ -709,7 +709,7 @@ impl FromStr for Color { fn from_str(s: &str) -> Result { 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())) } } diff --git a/src/lib.rs b/src/lib.rs index 24a852d9..50193b15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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.