Skip to content

Commit

Permalink
Merge pull request #69 from JarrettBillingsley/master
Browse files Browse the repository at this point in the history
Customizable level label colors
  • Loading branch information
Drakulix committed Dec 17, 2020
2 parents 195361c + 2e6e990 commit bfb70b8
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 9 deletions.
22 changes: 22 additions & 0 deletions examples/custom_colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use log::*;
use simplelog::*;

#[cfg(feature = "termcolor")]
fn main() {
let config = ConfigBuilder::new()
.set_level_color(Level::Error, Color::Magenta)
.set_level_color(Level::Trace, Color::Green)
.build();

TermLogger::init(LevelFilter::Trace, config, TerminalMode::Stdout).unwrap();
error!("Magenta error");
warn!("Yellow warning");
info!("Blue info");
debug!("Cyan debug");
trace!("Green trace");
}

#[cfg(not(feature = "termcolor"))]
fn main() {
println!("this example requires the termcolor feature.");
}
17 changes: 17 additions & 0 deletions examples/default_colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use log::*;
use simplelog::*;

#[cfg(feature = "termcolor")]
fn main() {
TermLogger::init(LevelFilter::Trace, Config::default(), TerminalMode::Stdout).unwrap();
error!("Red error");
warn!("Yellow warning");
info!("Blue info");
debug!("Cyan debug");
trace!("White trace");
}

#[cfg(not(feature = "termcolor"))]
fn main() {
println!("this example requires the termcolor feature.");
}
25 changes: 25 additions & 0 deletions examples/rgb_colors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use log::*;
use simplelog::*;

#[cfg(all(not(target_family = "windows"), feature = "termcolor"))]
fn main() {
let config = ConfigBuilder::new()
.set_level_color(Level::Error, Color::Rgb(191, 0, 0))
.set_level_color(Level::Warn, Color::Rgb(255, 127, 0))
.set_level_color(Level::Info, Color::Rgb(192, 192, 0))
.set_level_color(Level::Debug, Color::Rgb(63, 127, 0))
.set_level_color(Level::Trace, Color::Rgb(127, 127, 255))
.build();

TermLogger::init(LevelFilter::Trace, config, TerminalMode::Stdout).unwrap();
error!("Red error");
warn!("Orange warning");
info!("Yellow info");
debug!("Dark green debug");
trace!("Light blue trace");
}

#[cfg(any(target_family = "windows", not(feature = "termcolor")))]
fn main() {
println!("this example requires the termcolor feature and a non-Windows OS.");
}
23 changes: 22 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use log::LevelFilter;
use log::{Level, LevelFilter};

pub use chrono::offset::{FixedOffset, Local, Offset, TimeZone, Utc};
#[cfg(feature = "termcolor")]
use termcolor::Color;
use std::borrow::Cow;

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -61,6 +63,8 @@ pub struct Config {
pub(crate) time_local: bool,
pub(crate) filter_allow: Cow<'static, [Cow<'static, str>]>,
pub(crate) filter_ignore: Cow<'static, [Cow<'static, str>]>,
#[cfg(feature = "termcolor")]
pub(crate) level_color: [Color; 6],
}

/// Builder for the Logger Configurations (`Config`)
Expand Down Expand Up @@ -133,6 +137,13 @@ impl ConfigBuilder {
self
}

/// Set the color used for printing the level (if the logger supports it)
#[cfg(feature = "termcolor")]
pub fn set_level_color<'a>(&'a mut self, level: Level, color: Color) -> &'a mut ConfigBuilder {
self.0.level_color[level as usize] = color;
self
}

/// Set time chrono [strftime] format string.
///
/// [strftime]: https://docs.rs/chrono/0.4.0/chrono/format/strftime/index.html#specifiers
Expand Down Expand Up @@ -250,6 +261,16 @@ impl Default for Config {
time_local: false,
filter_allow: Cow::Borrowed(&[]),
filter_ignore: Cow::Borrowed(&[]),

#[cfg(feature = "termcolor")]
level_color: [
Color::White, // (dummy)
Color::Red, // Error
Color::Yellow, // Warn
Color::Blue, // Info
Color::Cyan, // Debug
Color::White, // Trace
],
}
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub use self::loggers::TestLogger;
pub use self::loggers::{CombinedLogger, SimpleLogger, WriteLogger};
#[cfg(feature = "termcolor")]
pub use self::loggers::{TermLogError, TermLogger, TerminalMode};
#[cfg(feature = "termcolor")]
pub use termcolor::Color;

pub use log::{Level, LevelFilter};

Expand Down
10 changes: 2 additions & 8 deletions src/loggers/termlog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::fmt;
use std::io::{Error, Write};
use std::sync::Mutex;
use termcolor;
use termcolor::{StandardStream, ColorChoice, Color, WriteColor, ColorSpec};
use termcolor::{StandardStream, ColorChoice, WriteColor, ColorSpec};

use self::TermLogError::{SetLogger};
use super::logging::*;
Expand Down Expand Up @@ -170,13 +170,7 @@ impl TermLogger {
record: &Record<'_>,
term_lock: &mut Box<dyn WriteColor + Send>,
) -> Result<(), Error> {
let color = match record.level() {
Level::Error => Color::Red,
Level::Warn => Color::Yellow,
Level::Info => Color::Blue,
Level::Debug => Color::Cyan,
Level::Trace => Color::White,
};
let color = self.config.level_color[record.level() as usize];

if self.config.time <= record.level() && self.config.time != LevelFilter::Off {
write_time(&mut *term_lock, &self.config)?;
Expand Down

0 comments on commit bfb70b8

Please sign in to comment.