darpan 0.2.4

Linux developer service monitoring utility with auto-detection, real-time health checks, and interactive TUI for databases, APIs, Docker containers, and more
Documentation
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Represents a single log entry from any source
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogEntry {
    /// When the log was generated
    pub timestamp: DateTime<Utc>,
    
    /// Log level/severity
    pub level: LogLevel,
    
    /// The actual log message
    pub message: String,
    
    /// Where this log came from
    pub source: LogSource,
    
    /// Optional: Raw log line (for preserving ANSI colors)
    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
    }
}

/// Log severity level
#[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",
        }
    }
}

/// Where a log entry came from
#[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",
        }
    }
}