From 83f92e2c20328f89257e64ec5b76d400461b0174 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Mon, 10 Jan 2022 14:24:19 +0100 Subject: [PATCH] Per logger output color config (#930) --- bee-common/bee-common/src/logger/config.rs | 35 ++++++------ bee-common/bee-common/src/logger/mod.rs | 63 +++++++++++----------- 2 files changed, 47 insertions(+), 51 deletions(-) diff --git a/bee-common/bee-common/src/logger/config.rs b/bee-common/bee-common/src/logger/config.rs index 25fcf89e5a..9367e9a4ca 100644 --- a/bee-common/bee-common/src/logger/config.rs +++ b/bee-common/bee-common/src/logger/config.rs @@ -8,8 +8,6 @@ use serde::Deserialize; use std::borrow::Cow; -/// Default value for the color flag. -const DEFAULT_COLOR_ENABLED: bool = true; /// Default value for the target width. const DEFAULT_TARGET_WIDTH: usize = 42; /// Default value for the level width. @@ -17,7 +15,9 @@ const DEFAULT_LEVEL_WIDTH: usize = 5; /// Default name for an output. const DEFAULT_OUTPUT_NAME: &str = LOGGER_STDOUT_NAME; /// Default log level for an output. -const DEFAULT_OUTPUT_LEVEL: LevelFilter = LevelFilter::Info; +const DEFAULT_OUTPUT_LEVEL_FILTER: LevelFilter = LevelFilter::Info; +/// Default value for the color flag. +const DEFAULT_COLOR_ENABLED: bool = true; /// Builder for a logger output configuration. #[derive(Default, Deserialize)] @@ -30,6 +30,8 @@ pub struct LoggerOutputConfigBuilder { target_filters: Option>, /// Log target exclusions of an output. target_exclusions: Option>, + /// Color flag of an output. + color_enabled: Option, } impl LoggerOutputConfigBuilder { @@ -57,11 +59,17 @@ impl LoggerOutputConfigBuilder { self } + /// Sets the color flag of a logger output. + pub fn color_enabled(mut self, color: bool) -> Self { + self.color_enabled.replace(color); + self + } + /// Builds a logger output configuration. pub fn finish(self) -> LoggerOutputConfig { LoggerOutputConfig { name: self.name.unwrap_or_else(|| DEFAULT_OUTPUT_NAME.to_owned()), - level_filter: self.level_filter.unwrap_or(DEFAULT_OUTPUT_LEVEL), + level_filter: self.level_filter.unwrap_or(DEFAULT_OUTPUT_LEVEL_FILTER), target_filters: self .target_filters .unwrap_or_else(Vec::new) @@ -74,6 +82,7 @@ impl LoggerOutputConfigBuilder { .iter() .map(|f| f.to_lowercase()) .collect(), + color_enabled: self.color_enabled.unwrap_or(DEFAULT_COLOR_ENABLED), } } } @@ -89,13 +98,13 @@ pub struct LoggerOutputConfig { pub(crate) target_filters: Vec, /// Log target exclusions of the output. pub(crate) target_exclusions: Vec, + /// Color flag of the output. + pub(crate) color_enabled: bool, } /// Builder for a logger configuration. #[derive(Default, Deserialize)] pub struct LoggerConfigBuilder { - /// Color flag of the logger. - color_enabled: Option, /// Width of the target section of a log. target_width: Option, /// Width of the level section of a log. @@ -105,12 +114,6 @@ pub struct LoggerConfigBuilder { } impl LoggerConfigBuilder { - /// Sets the color flag of a logger. - pub fn color_enabled(mut self, color: bool) -> Self { - self.color_enabled.replace(color); - self - } - /// Sets the target width. pub fn with_target_width(mut self, width: usize) -> Self { self.target_width.replace(width); @@ -151,7 +154,6 @@ impl LoggerConfigBuilder { .unwrap_or_default(); LoggerConfig { - color_enabled: self.color_enabled.unwrap_or(DEFAULT_COLOR_ENABLED), target_width: self.target_width.unwrap_or(DEFAULT_TARGET_WIDTH), level_width: self.level_width.unwrap_or(DEFAULT_LEVEL_WIDTH), outputs, @@ -162,8 +164,6 @@ impl LoggerConfigBuilder { /// Logger configuration. #[derive(Clone)] pub struct LoggerConfig { - /// Color flag of the logger. - pub(crate) color_enabled: bool, /// Width of the target section of a log. pub(crate) target_width: usize, /// Width of the level section of a log. @@ -178,11 +178,6 @@ impl LoggerConfig { LoggerConfigBuilder::default() } - /// Returns the color flag of the `LoggerConfig`. - pub fn color_enabled(&self) -> bool { - self.color_enabled - } - /// Returns the width of the target section of the `LoggerConfig`. pub fn target_width(&self) -> usize { self.target_width diff --git a/bee-common/bee-common/src/logger/mod.rs b/bee-common/bee-common/src/logger/mod.rs index 7d370c8216..4a2e8b886c 100644 --- a/bee-common/bee-common/src/logger/mod.rs +++ b/bee-common/bee-common/src/logger/mod.rs @@ -51,40 +51,41 @@ pub fn logger_init(config: LoggerConfig) -> Result<(), Error> { let target_width = config.target_width; let level_width = config.level_width; - let mut logger = if config.color_enabled { - let colors = ColoredLevelConfig::new() - .trace(Color::BrightMagenta) - .debug(Color::BrightBlue) - .info(Color::BrightGreen) - .warn(Color::BrightYellow) - .error(Color::BrightRed); - - // Creates a logger dispatch with color support. - Dispatch::new().format(move |out, message, record| { - out.finish(log_format!( - record.target(), - colors.color(record.level()), - message, - target_width, - level_width - )) - }) - } else { - // Creates a logger dispatch without color support. - Dispatch::new().format(move |out, message, record| { - out.finish(log_format!( - record.target(), - record.level(), - message, - target_width, - level_width - )) - }) - }; + let mut logger = Dispatch::new(); for output in config.outputs { // Creates a logger dispatch for each output of the configuration. - let mut dispatch = Dispatch::new().level(output.level_filter); + let mut dispatch = if output.color_enabled { + let colors = ColoredLevelConfig::new() + .trace(Color::BrightMagenta) + .debug(Color::BrightBlue) + .info(Color::BrightGreen) + .warn(Color::BrightYellow) + .error(Color::BrightRed); + + // Creates a logger dispatch with color support. + Dispatch::new().format(move |out, message, record| { + out.finish(log_format!( + record.target(), + colors.color(record.level()), + message, + target_width, + level_width + )) + }) + } else { + // Creates a logger dispatch without color support. + Dispatch::new().format(move |out, message, record| { + out.finish(log_format!( + record.target(), + record.level(), + message, + target_width, + level_width + )) + }) + } + .level(output.level_filter); if !output.target_filters.is_empty() { let target_filters = output.target_filters;