From 204b7087014109c9151db68db825346307f7707f Mon Sep 17 00:00:00 2001 From: twiby Date: Tue, 21 Nov 2023 15:02:20 +0100 Subject: [PATCH 1/2] provide public API to set up the logger entirely without calling `log::` methods --- src/lib.rs | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) 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() { From b8c149bdec0a400e5855d05057d49e333d475ae9 Mon Sep 17 00:00:00 2001 From: twiby Date: Wed, 22 Nov 2023 13:40:14 +0100 Subject: [PATCH 2/2] update README --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) 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 -------