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:
vhaudiquet committed 2026-11-17 12:46:16 +01:00
1 parent 3244f1c3b3
commit 05fed64629
1 file changed
+16 -1
+16 -1
View File
@@ -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();