2023-07-17 21:23:59 +02:00
|
|
|
use colored::Colorize;
|
2023-07-17 20:40:01 +02:00
|
|
|
use fern::Dispatch;
|
2023-07-17 21:23:59 +02:00
|
|
|
use log::SetLoggerError;
|
2023-07-17 20:40:01 +02:00
|
|
|
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()));
|
2023-07-17 20:40:01 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
"command:stderr" => {
|
2023-07-17 21:23:59 +02:00
|
|
|
out.finish(format_args!("{} {}", "!!".red(), message.to_string()));
|
2023-07-17 20:40:01 +02:00
|
|
|
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') {
|
2023-07-17 20:40:01 +02:00
|
|
|
'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()
|
|
|
|
},
|
2023-07-17 20:40:01 +02:00
|
|
|
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),
|
|
|
|
)
|
2023-07-17 20:40:01 +02:00
|
|
|
.chain(std::io::stdout())
|
|
|
|
.apply()
|
|
|
|
}
|