Color log output

Color as follows:

    * Debug output -> Cyan
    * Warning, errors -> Red, with the type (WARN; ERROR) red blinking
    * All other output yellow.
This commit is contained in:
Matthias Beyer 2016-05-15 17:38:02 +02:00
parent 344296487d
commit 376a23b8ce

View file

@ -24,14 +24,37 @@ impl Log for ImagLogger {
}
fn log(&self, record: &LogRecord) {
use ansi_term::Colour::Red;
use ansi_term::Colour::Yellow;
use ansi_term::Colour::Cyan;
if self.enabled(record.metadata()) {
// TODO: This is just simple logging. Maybe we can enhance this lateron
if record.metadata().level() == LogLevel::Debug {
let loc = record.location();
writeln!(stderr(), "[imag][{: <5}][{}][{: >5}]: {}",
record.level(), loc.file(), loc.line(), record.args()).ok();
} else {
writeln!(stderr(), "[imag][{: <5}]: {}", record.level(), record.args()).ok();
let loc = record.location();
match record.metadata().level() {
LogLevel::Debug => {
let lvl = Cyan.paint(format!("{}", record.level()));
let file = Cyan.paint(format!("{}", loc.file()));
let ln = Cyan.paint(format!("{}", loc.line()));
let args = Cyan.paint(format!("{}", record.args()));
writeln!(stderr(), "[imag][{: <5}][{}][{: >5}]: {}", lvl, file, ln, args).ok();
},
LogLevel::Warn | LogLevel::Error => {
let lvl = Red.blink().paint(format!("{}", record.level()));
let args = Red.paint(format!("{}", record.args()));
writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok();
},
LogLevel::Info => {
let lvl = Yellow.paint(format!("{}", record.level()));
let args = Yellow.paint(format!("{}", record.args()));
writeln!(stderr(), "[imag][{: <5}]: {}", lvl, args).ok();
},
_ => {
writeln!(stderr(), "[imag][{: <5}]: {}", record.level(), record.args()).ok();
},
}
}
}