65 lines
2.2 KiB
Rust
65 lines
2.2 KiB
Rust
use colored::Colorize;
|
|
use fern::Dispatch;
|
|
use log::{Record, SetLoggerError};
|
|
use std::env;
|
|
|
|
fn format_regular<S: Into<String>>(log: S, record: &Record) -> String {
|
|
let log = log.into();
|
|
let line_prefix = |line: String, extend: bool| {
|
|
let prefix = if extend {
|
|
match record.level() {
|
|
log::Level::Trace => " ]".bright_blue(),
|
|
log::Level::Debug => " ?".green(),
|
|
log::Level::Info => " >".blue(),
|
|
log::Level::Warn => " #".yellow(),
|
|
log::Level::Error => " !".red(),
|
|
}.to_string()
|
|
} else {
|
|
match record.level() {
|
|
log::Level::Trace => "[TRACE]".bright_blue(),
|
|
log::Level::Debug => "??".green(),
|
|
log::Level::Info => "=>".blue(),
|
|
log::Level::Warn => "##".yellow(),
|
|
log::Level::Error => "!!".red()
|
|
}.to_string()
|
|
};
|
|
return format!("{} {}", prefix, line);
|
|
};
|
|
|
|
let mut lines = log.lines().peekable();
|
|
let mut output = match lines.peek() {
|
|
Some(_line) => line_prefix(lines.next().unwrap().to_string(), false),
|
|
None => return "".to_string(),
|
|
};
|
|
|
|
for line in lines {
|
|
output.push_str(&*format!("\n{}", line_prefix(line.to_string(), true)));
|
|
}
|
|
|
|
output
|
|
}
|
|
|
|
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!("{}", format_regular(message.to_string(), record))),
|
|
}
|
|
})
|
|
.level(
|
|
env::var("PKGR_LOG_LEVEL")
|
|
.unwrap_or_else(|_| "info".to_string())
|
|
.parse()
|
|
.unwrap_or(log::LevelFilter::Info),
|
|
)
|
|
.chain(std::io::stdout())
|
|
.apply()
|
|
}
|