Skip to content

Commit

Permalink
Merge pull request #18 from baoyachi/feature/15
Browse files Browse the repository at this point in the history
support color output with terminal
  • Loading branch information
baoyachi authored Oct 27, 2021
2 parents fb59d34 + 9cfd0ca commit 2b693d5
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "simple-log"
version = "1.2.0"
version = "1.3.0"
authors = ["baoyachi <[email protected]>"]
description = "A simple log"
edition = "2018"
Expand Down
16 changes: 11 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ fn build_config(log: &LogConfig) -> SimpleResult<Config> {
}
OutKind::Console => {
let console = ConsoleAppender::builder()
.encoder(Box::new(encoder(log.time_format.as_ref())))
.encoder(Box::new(encoder(log.time_format.as_ref(), true)))
.build();
config_builder = config_builder
.appender(Appender::builder().build(SIMPLE_LOG_CONSOLE, Box::new(console)));
Expand Down Expand Up @@ -587,14 +587,20 @@ fn init_default_log(log: &mut LogConfig) {
}
}

fn encoder(time_format: Option<&String>) -> PatternEncoder {
fn encoder(time_format: Option<&String>, color: bool) -> PatternEncoder {
let time_format = if let Some(format) = time_format {
format.to_string()
} else {
DEFAULT_DATE_TIME_FORMAT.to_string()
};
let mut pattern = format!("{{d({})}} ", time_format);
pattern += "[{l}] <{M}:{L}>:{m}\n";

let color_level = match color {
true => "h({l})",
false => "l",
};

let mut pattern = format!("{{d({})}} [{{{}}}] ", time_format, color_level);
pattern += "<{M}:{L}>:{m}\n";
PatternEncoder::new(pattern.as_str())
}

Expand All @@ -609,7 +615,7 @@ fn file_appender(log: &LogConfig) -> SimpleResult<Box<RollingFileAppender>> {
let policy = CompoundPolicy::new(Box::new(trigger), Box::new(roll));

let logfile = RollingFileAppender::builder()
.encoder(Box::new(encoder(log.time_format.as_ref())))
.encoder(Box::new(encoder(log.time_format.as_ref(), false)))
.build(log.path.clone(), Box::new(policy))
.map_err(|e| e.to_string())?;

Expand Down

0 comments on commit 2b693d5

Please sign in to comment.