Skip to content

Commit

Permalink
Per logger output color config (#930)
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez authored Jan 10, 2022
1 parent 3610458 commit 83f92e2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 51 deletions.
35 changes: 15 additions & 20 deletions bee-common/bee-common/src/logger/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ 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.
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)]
Expand All @@ -30,6 +30,8 @@ pub struct LoggerOutputConfigBuilder {
target_filters: Option<Vec<String>>,
/// Log target exclusions of an output.
target_exclusions: Option<Vec<String>>,
/// Color flag of an output.
color_enabled: Option<bool>,
}

impl LoggerOutputConfigBuilder {
Expand Down Expand Up @@ -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)
Expand All @@ -74,6 +82,7 @@ impl LoggerOutputConfigBuilder {
.iter()
.map(|f| f.to_lowercase())
.collect(),
color_enabled: self.color_enabled.unwrap_or(DEFAULT_COLOR_ENABLED),
}
}
}
Expand All @@ -89,13 +98,13 @@ pub struct LoggerOutputConfig {
pub(crate) target_filters: Vec<String>,
/// Log target exclusions of the output.
pub(crate) target_exclusions: Vec<String>,
/// 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<bool>,
/// Width of the target section of a log.
target_width: Option<usize>,
/// Width of the level section of a log.
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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.
Expand All @@ -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
Expand Down
63 changes: 32 additions & 31 deletions bee-common/bee-common/src/logger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 83f92e2

Please sign in to comment.