parent
dc2dfc4bbc
commit
f6ab14470b
@ -1,84 +1,34 @@
|
|||||||
use std::boxed::Box;
|
use std::path::PathBuf;
|
||||||
use std::error::Error;
|
|
||||||
use std::fmt;
|
|
||||||
|
|
||||||
|
use chrono::Local;
|
||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
use log::*;
|
use fern::{Dispatch, log_file};
|
||||||
|
use fern::colors::{Color, ColoredLevelConfig};
|
||||||
|
use log::LevelFilter;
|
||||||
|
|
||||||
use crate::result::Result;
|
use crate::result::Result;
|
||||||
|
|
||||||
static LOGGER: Logger = Logger;
|
pub fn setup_logger(logfile: PathBuf, level: LevelFilter) -> Result<()> {
|
||||||
|
Dispatch::new()
|
||||||
#[derive(Debug)]
|
.format(|out, message, record| {
|
||||||
pub enum LoggerError {
|
let colors = ColoredLevelConfig::new()
|
||||||
SetLogger(SetLoggerError),
|
.error(Color::Red)
|
||||||
}
|
.warn(Color::Magenta)
|
||||||
|
.info(Color::Cyan)
|
||||||
impl From<SetLoggerError> for LoggerError {
|
.debug(Color::Yellow)
|
||||||
fn from(e: SetLoggerError) -> Self {
|
.trace(Color::Green);
|
||||||
LoggerError::SetLogger(e)
|
|
||||||
}
|
out.finish(format_args!(
|
||||||
}
|
"{} {} {}",
|
||||||
|
Local::now().format("%Y-%m-%dT%H:%M:%S%.3f%z").to_string().white().bold(),
|
||||||
impl Error for LoggerError {
|
colors.color(record.level()),
|
||||||
fn description(&self) -> &str {
|
message
|
||||||
match *self {
|
))
|
||||||
LoggerError::SetLogger(_) => "Logger already initialized",
|
})
|
||||||
}
|
.level(level)
|
||||||
}
|
.chain(std::io::stdout())
|
||||||
fn cause(&self) -> Option<&dyn Error> {
|
.chain(log_file(logfile)?)
|
||||||
match *self {
|
.apply()?;
|
||||||
LoggerError::SetLogger(_) => Some(self),
|
|
||||||
}
|
Ok(())
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue