use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
pub timestamp: DateTime<Utc>,
pub level: LogLevel,
pub message: String,
pub source: LogSource,
pub raw: Option<String>,
}
impl LogEntry {
pub fn new(level: LogLevel, message: String, source: LogSource) -> Self {
Self {
timestamp: Utc::now(),
level,
message,
source,
raw: None,
}
}
pub fn with_timestamp(mut self, timestamp: DateTime<Utc>) -> Self {
self.timestamp = timestamp;
self
}
pub fn with_raw(mut self, raw: String) -> Self {
self.raw = Some(raw);
self
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "UPPERCASE")]
pub enum LogLevel {
Debug,
Info,
Warn,
Error,
}
impl LogLevel {
pub fn from_priority(priority: i32) -> Self {
match priority {
0..=3 => LogLevel::Error,
4 => LogLevel::Warn,
5..=6 => LogLevel::Info,
_ => LogLevel::Debug,
}
}
pub fn color(&self) -> ratatui::style::Color {
use ratatui::style::Color;
match self {
LogLevel::Debug => Color::DarkGray,
LogLevel::Info => Color::White,
LogLevel::Warn => Color::Yellow,
LogLevel::Error => Color::Red,
}
}
pub fn as_str(&self) -> &str {
match self {
LogLevel::Debug => "DEBUG",
LogLevel::Info => "INFO ",
LogLevel::Warn => "WARN ",
LogLevel::Error => "ERROR",
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
pub enum LogSource {
Docker,
Systemd,
Process,
File,
}
impl LogSource {
pub fn as_str(&self) -> &str {
match self {
LogSource::Docker => "Docker",
LogSource::Systemd => "Systemd",
LogSource::Process => "Process",
LogSource::File => "File",
}
}
}