packager/pkgr/src/logging.rs

58 lines
2 KiB
Rust
Raw Normal View History

use log::SetLoggerError;
use fern::Dispatch;
use std::env;
use colored::Colorize;
pub fn setup_logger() -> Result<(), SetLoggerError> {
Dispatch::new()
.format(|out, message, record| {
match record.metadata().target() {
"command:stdout" => {
out.finish(format_args!(
"{} {}",
"::".cyan(),
message.to_string()
));
return;
}
"command:stderr" => {
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 {
match record
.level()
.to_string()
.chars()
.nth(0)
.unwrap_or('T')
{
'T' => "[TRACE]".bright_blue(),
'D' => "??".green(),
'I' => "=>".blue(),
'W' => "##".yellow(),
'E' => "!!".red(),
_ => "**".blue(),
}.to_string()
} else { "".to_string() },
message.to_string().bright_white()
))
}
}
})
.level(env::var("PKGR_LOG_LEVEL")
.unwrap_or_else(|_| "info".to_string())
.parse()
.unwrap_or(log::LevelFilter::Info))
.chain(std::io::stdout())
.apply()
}