packager/pkgr/src/logging.rs

49 lines
1.7 KiB
Rust
Raw Normal View History

2023-07-17 21:23:59 +02:00
use colored::Colorize;
use fern::Dispatch;
2023-07-17 21:23:59 +02:00
use log::SetLoggerError;
use std::env;
pub fn setup_logger() -> Result<(), SetLoggerError> {
Dispatch::new()
.format(|out, message, record| {
match record.metadata().target() {
"command:stdout" => {
2023-07-17 21:23:59 +02:00
out.finish(format_args!("{} {}", "::".cyan(), message.to_string()));
return;
}
"command:stderr" => {
2023-07-17 21:23:59 +02:00
out.finish(format_args!("{} {}", "!!".red(), message.to_string()));
return;
}
_ => {
out.finish(format_args!(
"{} {}",
// Some logic so messages look nice
if message.to_string().len() > 0 {
2023-07-17 21:23:59 +02:00
match record.level().to_string().chars().nth(0).unwrap_or('T') {
'T' => "[TRACE]".bright_blue(),
'D' => "??".green(),
'I' => "=>".blue(),
'W' => "##".yellow(),
'E' => "!!".red(),
_ => "**".blue(),
2023-07-17 21:23:59 +02:00
}
.to_string()
} else {
"".to_string()
},
message.to_string().bright_white()
))
}
}
})
2023-07-17 21:23:59 +02:00
.level(
env::var("PKGR_LOG_LEVEL")
.unwrap_or_else(|_| "info".to_string())
.parse()
.unwrap_or(log::LevelFilter::Info),
)
.chain(std::io::stdout())
.apply()
}