#[derive(Debug, Clone, PartialEq)]
pub enum LogLevel {
Debug,
Info,
Warning,
Error,
Critical,
}
impl LogLevel {
pub fn from_str(s: &str) -> Self {
match s.to_lowercase().as_str() {
"debug" => LogLevel::Debug,
"info" => LogLevel::Info,
"warn" | "warning" => LogLevel::Warning,
"error" => LogLevel::Error,
"critical" | "crit" | "fatal" => LogLevel::Critical,
_ => LogLevel::Info,
}
}
pub fn as_str(&self) -> &str {
match self {
LogLevel::Debug => "debug",
LogLevel::Info => "info",
LogLevel::Warning => "warning",
LogLevel::Error => "error",
LogLevel::Critical => "critical",
}
}
}
#[derive(Debug, Clone)]
pub struct LogEntry {
pub level: LogLevel,
pub message: String,
pub timestamp: u64,
pub source: Option<String>,
}
impl LogEntry {
pub fn new(level: LogLevel, message: &str) -> Self {
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
Self {
level,
message: message.to_string(),
timestamp,
source: None,
}
}
pub fn with_source(mut self, source: &str) -> Self {
self.source = Some(source.to_string());
self
}
}