parent
dc2dfc4bbc
commit
f6ab14470b
@ -1,84 +1,34 @@
|
||||
use std::boxed::Box;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use chrono::Local;
|
||||
use colored::Colorize;
|
||||
use log::*;
|
||||
use fern::{Dispatch, log_file};
|
||||
use fern::colors::{Color, ColoredLevelConfig};
|
||||
use log::LevelFilter;
|
||||
|
||||
use crate::result::Result;
|
||||
|
||||
static LOGGER: Logger = Logger;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum LoggerError {
|
||||
SetLogger(SetLoggerError),
|
||||
}
|
||||
|
||||
impl From<SetLoggerError> for LoggerError {
|
||||
fn from(e: SetLoggerError) -> Self {
|
||||
LoggerError::SetLogger(e)
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for LoggerError {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
LoggerError::SetLogger(_) => "Logger already initialized",
|
||||
}
|
||||
}
|
||||
fn cause(&self) -> Option<&dyn Error> {
|
||||
match *self {
|
||||
LoggerError::SetLogger(_) => Some(self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for LoggerError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
use std::error::Error as FmtError;
|
||||
write!(f, "{}", self.description())
|
||||
}
|
||||
}
|
||||
|
||||
struct Logger;
|
||||
|
||||
impl Log for Logger {
|
||||
fn enabled(&self, metadata: &Metadata) -> bool {
|
||||
metadata.level() <= Level::Trace
|
||||
}
|
||||
|
||||
fn log(&self, record: &Record) {
|
||||
if self.enabled(record.metadata()) {
|
||||
let now = chrono::Local::now();
|
||||
let ts = now.format("%Y-%m-%dT%H:%M:%S%.3f%z").to_string();
|
||||
|
||||
let msg = match record.level() {
|
||||
Level::Error => format!("{} {} {}", ts.white().bold(), "ERR".red(), record.args()),
|
||||
Level::Warn => {
|
||||
format!("{} {} {}", ts.white().bold(), "WRN".purple(), record.args())
|
||||
}
|
||||
Level::Info => format!("{} {} {}", ts.white().bold(), "INF".cyan(), record.args()),
|
||||
Level::Debug => {
|
||||
format!("{} {} {}", ts.white().bold(), "DBG".yellow(), record.args())
|
||||
}
|
||||
Level::Trace => {
|
||||
format!("{} {} {}", ts.white().bold(), "TRC".green(), record.args())
|
||||
}
|
||||
};
|
||||
println!("{}", msg);
|
||||
}
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
pub fn init() -> Result<()> {
|
||||
match set_logger(&LOGGER).map(|()| set_max_level(LevelFilter::Info)) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(e) => Err(Box::new(LoggerError::from(e))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_level(level: LevelFilter) {
|
||||
set_max_level(level);
|
||||
pub fn setup_logger(logfile: PathBuf, level: LevelFilter) -> Result<()> {
|
||||
Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
let colors = ColoredLevelConfig::new()
|
||||
.error(Color::Red)
|
||||
.warn(Color::Magenta)
|
||||
.info(Color::Cyan)
|
||||
.debug(Color::Yellow)
|
||||
.trace(Color::Green);
|
||||
|
||||
out.finish(format_args!(
|
||||
"{} {} {}",
|
||||
Local::now().format("%Y-%m-%dT%H:%M:%S%.3f%z").to_string().white().bold(),
|
||||
colors.color(record.level()),
|
||||
message
|
||||
))
|
||||
})
|
||||
.level(level)
|
||||
.chain(std::io::stdout())
|
||||
.chain(log_file(logfile)?)
|
||||
.apply()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
Reference in new issue