cli: prefix and color warnings and errors in the log output
The logger format printed every record as a bare message, so a warning read like a stray lowercase fragment and an error like plain progress output. WARN: and ERROR: prefixes, yellow and red on a terminal, bare when piped; the other levels keep the bare message, which progress lines read better without.
This commit is contained in:
1 parent
3244f1c3b3
commit
05fed64629
1 file changed
+16
-1
+16
-1
@@ -195,7 +195,22 @@ fn main() {
|
||||
let logger =
|
||||
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info"))
|
||||
.format_timestamp(None)
|
||||
.format(|buf, record| writeln!(buf, "{}", record.args()))
|
||||
.format(|buf, record| {
|
||||
let message = record.args().to_string();
|
||||
match record.level() {
|
||||
// Warnings and errors carry their level, colored for a
|
||||
// terminal (bare when piped); the other levels keep the
|
||||
// bare message, progress lines read better without a
|
||||
// prefix
|
||||
log::Level::Warn => {
|
||||
writeln!(buf, "{}", ansi_wrap("33", &format!("WARN: {message}")))
|
||||
}
|
||||
log::Level::Error => {
|
||||
writeln!(buf, "{}", ansi_wrap("31", &format!("ERROR: {message}")))
|
||||
}
|
||||
_ => writeln!(buf, "{message}"),
|
||||
}
|
||||
})
|
||||
.build();
|
||||
let multi = indicatif::MultiProgress::new();
|
||||
LogWrapper::new(multi.clone(), logger).try_init().unwrap();
|
||||
|
||||
Reference in new issue
Block a user