Files
unshell/src/logger/pretty_logger.rs
T

59 lines
1.6 KiB
Rust
Raw Normal View History

2025-11-09 12:34:52 -07:00
use chrono::{DateTime, Utc};
use crate::logger::{LogLevel, Logger, Record};
2025-12-13 13:29:17 -07:00
pub struct PrettyLogger {
output: Option<Box<dyn Fn(&Record)>>,
}
2025-11-09 12:34:52 -07:00
// static TRACE_COLOR: &str = "\x1b[34m";
static DEBUG_COLOR: &str = "\x1b[36m";
static INFO_COLOR: &str = "\x1b[32m";
static WARN_COLOR: &str = "\x1b[33m";
static ERROR_COLOR: &str = "\x1b[31m";
static WHITE: &str = "\x1b[97m";
static OFF_WHITE: &str = "\x1b[37m";
static TIME_COLOR: &str = "\x1b[36m";
static GREY: &str = "\x1b[90m";
impl Logger for PrettyLogger {
fn log(&self, message: Record) {
2025-12-13 13:29:17 -07:00
if let Some(ref func) = self.output {
(*func)(&message)
}
2025-11-09 12:34:52 -07:00
let log_level = match message.log_level {
2025-11-10 22:18:21 -07:00
LogLevel::Debug => format!("{DEBUG_COLOR}DBUG"),
2025-11-09 12:34:52 -07:00
LogLevel::Info => format!("{INFO_COLOR}INFO"),
LogLevel::Warn => format!("{WARN_COLOR}WARN"),
2025-11-10 22:18:21 -07:00
LogLevel::Error => format!("{ERROR_COLOR}ERR!"),
2025-11-09 12:34:52 -07:00
};
let date: DateTime<Utc> = message.time.into();
let date = date.to_rfc2822().to_string();
2025-11-11 11:00:28 -07:00
let location = message.location.unwrap_or("".to_string());
2025-11-09 12:34:52 -07:00
println!(
"{OFF_WHITE}[{TIME_COLOR}{}{OFF_WHITE}] {} {WHITE}{} {GREY}{}{WHITE}",
2025-11-11 11:00:28 -07:00
date, log_level, message.message, location
2025-11-09 12:34:52 -07:00
);
}
}
impl PrettyLogger {
pub fn init() {
2025-12-13 13:29:17 -07:00
crate::logger::set_logger_box(Box::new(PrettyLogger { output: None }));
}
pub fn init_output<T>(output: T)
where
T: Fn(&Record) + 'static,
{
crate::logger::set_logger_box(Box::new(PrettyLogger {
output: Some(Box::new(output)),
}));
2025-11-09 12:34:52 -07:00
}
}