#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum LogLevel {
Debug,
Info,
Warn,
Error,
Critical,
}
impl std::fmt::Display for LogLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LogLevel::Debug => write!(f, "DEBUG"),
LogLevel::Info => write!(f, "INFO"),
LogLevel::Warn => write!(f, "WARN"),
LogLevel::Error => write!(f, "ERROR"),
LogLevel::Critical => write!(f, "CRITICAL"),
}
}
}
impl std::str::FromStr for LogLevel {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"debug" => Ok(LogLevel::Debug),
"info" => Ok(LogLevel::Info),
"warn" => Ok(LogLevel::Warn),
"error" => Ok(LogLevel::Error),
"critical" => Ok(LogLevel::Critical),
_ => Err(()),
}
}
}
impl LogLevel {
pub fn should_log(&self, level: LogLevel) -> bool {
level >= *self
}
pub fn short_name(&self) -> &'static str {
match self {
LogLevel::Debug => "D",
LogLevel::Info => "I",
LogLevel::Warn => "W",
LogLevel::Error => "E",
LogLevel::Critical => "C",
}
}
}