diff --git a/README.md b/README.md index 2cfa241..edd4aad 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,27 @@ Multiple features can be combined. features = ["colors", "threads", "timestamps", "nightly", "stderr"] ``` +Wrapping with another logger +---------------------------- + +Users that might want to wrap this logger to be able to catch log events for various +reasons can setup the logger as follows: + +On windows machines: +```rust +let logger = SimpleLogger::new(); +set_up_color_terminal(); +let max_level = logger.max_level(); +``` + +Otherwise: +```rust +let logger = SimpleLogger::new(); +let max_level = logger.max_level(); +``` + +The user can then themselves call `log::set_max_level` and `log::set_boxed_logger` or equivalent as they wish. + Licence ------- diff --git a/src/lib.rs b/src/lib.rs index c1f1b07..c26b96a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -341,24 +341,28 @@ impl SimpleLogger { self } - /// 'Init' the actual logger, instantiate it and configure it, - /// this method MUST be called in order for the logger to be effective. - pub fn init(mut self) -> Result<(), SetLoggerError> { - #[cfg(all(windows, feature = "colored"))] - set_up_color_terminal(); - + /// Configure the logger + pub fn max_level(&mut self) -> LevelFilter { /* Sort all module levels from most specific to least specific. The length of the module * name is used instead of its actual depth to avoid module name parsing. */ self.module_levels .sort_by_key(|(name, _level)| name.len().wrapping_neg()); let max_level = self.module_levels.iter().map(|(_name, level)| level).copied().max(); - let max_level = max_level + max_level .map(|lvl| lvl.max(self.default_level)) - .unwrap_or(self.default_level); + .unwrap_or(self.default_level) + } + + /// 'Init' the actual logger and instantiate it, + /// this method MUST be called in order for the logger to be effective. + pub fn init(mut self) -> Result<(), SetLoggerError> { + #[cfg(all(windows, feature = "colored"))] + set_up_color_terminal(); + + let max_level = self.max_level(); log::set_max_level(max_level); - log::set_boxed_logger(Box::new(self))?; - Ok(()) + log::set_boxed_logger(Box::new(self)) } } @@ -488,7 +492,7 @@ impl Log for SimpleLogger { } #[cfg(all(windows, feature = "colored"))] -fn set_up_color_terminal() { +pub fn set_up_color_terminal() { use std::io::{stdout, IsTerminal}; if stdout().is_terminal() {