Add ANSI colors to log levels: red E, yellow W, green I, cyan D
This commit is contained in:
parent
8526b0d585
commit
628f853c85
1 changed files with 18 additions and 7 deletions
|
|
@ -2,13 +2,20 @@ use env_logger::fmt::Formatter;
|
||||||
use log::Record;
|
use log::Record;
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
|
// ANSI color codes
|
||||||
|
const RED: &str = "\x1b[31m";
|
||||||
|
const YELLOW: &str = "\x1b[33m";
|
||||||
|
const GREEN: &str = "\x1b[32m";
|
||||||
|
const CYAN: &str = "\x1b[36m";
|
||||||
|
const RESET: &str = "\x1b[0m";
|
||||||
|
|
||||||
pub fn compact_format(buf: &mut Formatter, record: &Record) -> std::io::Result<()> {
|
pub fn compact_format(buf: &mut Formatter, record: &Record) -> std::io::Result<()> {
|
||||||
let level = match record.level() {
|
let (level, color) = match record.level() {
|
||||||
log::Level::Error => "E",
|
log::Level::Error => ("E", RED),
|
||||||
log::Level::Warn => "W",
|
log::Level::Warn => ("W", YELLOW),
|
||||||
log::Level::Info => "I",
|
log::Level::Info => ("I", GREEN),
|
||||||
log::Level::Debug => "D",
|
log::Level::Debug => ("D", CYAN),
|
||||||
log::Level::Trace => "T",
|
log::Level::Trace => ("T", ""),
|
||||||
};
|
};
|
||||||
|
|
||||||
let now = chrono::Local::now();
|
let now = chrono::Local::now();
|
||||||
|
|
@ -21,7 +28,11 @@ pub fn compact_format(buf: &mut Formatter, record: &Record) -> std::io::Result<(
|
||||||
target
|
target
|
||||||
};
|
};
|
||||||
|
|
||||||
writeln!(buf, "{} {} {}:{}", timestamp, level, module, record.args())
|
if color.is_empty() {
|
||||||
|
writeln!(buf, "{} {} {}:{}", timestamp, level, module, record.args())
|
||||||
|
} else {
|
||||||
|
writeln!(buf, "{} {}{}{} {}:{}", timestamp, color, level, RESET, module, record.args())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_compact_logger(default_filter: &str) {
|
pub fn init_compact_logger(default_filter: &str) {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue