baichun-framework-logger 0.1.0

Logger module for Baichun-Rust framework
Documentation
use serde::{Deserialize, Serialize};
use std::fmt;
use std::path::PathBuf;

/// 日志级别
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogLevel {
    /// 错误级别
    Error = 1,
    /// 警告级别
    Warn = 2,
    /// 信息级别
    Info = 3,
    /// 调试级别
    Debug = 4,
    /// 追踪级别
    Trace = 5,
}

impl From<LogLevel> for tracing::Level {
    fn from(level: LogLevel) -> Self {
        match level {
            LogLevel::Error => tracing::Level::ERROR,
            LogLevel::Warn => tracing::Level::WARN,
            LogLevel::Info => tracing::Level::INFO,
            LogLevel::Debug => tracing::Level::DEBUG,
            LogLevel::Trace => tracing::Level::TRACE,
        }
    }
}

impl fmt::Display for LogLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            LogLevel::Error => write!(f, "ERROR"),
            LogLevel::Warn => write!(f, "WARN"),
            LogLevel::Info => write!(f, "INFO"),
            LogLevel::Debug => write!(f, "DEBUG"),
            LogLevel::Trace => write!(f, "TRACE"),
        }
    }
}

/// 日志滚动策略
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum RollingPolicy {
    /// 按时间滚动
    Time(TimeRollingConfig),
    /// 按大小滚动
    Size(SizeRollingConfig),
    /// 混合滚动策略
    Compound {
        /// 时间滚动配置
        time: TimeRollingConfig,
        /// 大小滚动配置
        size: SizeRollingConfig,
    },
}

/// 时间滚动配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TimeRollingConfig {
    /// 滚动周期
    pub period: TimePeriod,
    /// 保留天数
    pub keep_days: u32,
}

/// 时间周期
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TimePeriod {
    /// 每小时
    Hourly,
    /// 每天
    Daily,
    /// 每周
    Weekly,
    /// 每月
    Monthly,
}

/// 大小滚动配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SizeRollingConfig {
    /// 最大文件大小(字节)
    pub max_size: u64,
    /// 最大文件数
    pub max_files: u32,
}

/// 日志格式
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum LogFormat {
    /// 文本格式
    Text(TextFormatConfig),
    /// JSON格式
    Json(JsonFormatConfig),
}

/// 文本格式配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TextFormatConfig {
    /// 时间格式
    pub time_format: String,
    /// 是否显示级别
    pub show_level: bool,
    /// 是否显示目标
    pub show_target: bool,
    /// 是否显示线程ID
    pub show_thread_id: bool,
    /// 是否显示文件名
    pub show_file: bool,
    /// 是否显示行号
    pub show_line: bool,
}

/// JSON格式配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JsonFormatConfig {
    /// 时间格式
    pub time_format: String,
    /// 是否美化输出
    pub pretty: bool,
    /// 是否包含调用者信息
    pub include_caller: bool,
    /// 是否包含线程信息
    pub include_thread: bool,
}

/// 日志配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggerConfig {
    /// 日志级别
    pub level: LogLevel,
    /// 日志目录
    pub dir: PathBuf,
    /// 日志文件名
    pub filename: String,
    /// 滚动策略
    pub rolling_policy: RollingPolicy,
    /// 日志格式
    pub format: LogFormat,
    /// 是否异步写入
    pub async_write: bool,
    /// 是否按级别分离文件
    pub split_by_level: bool,
    /// 控制台输出配置
    pub console: Option<ConsoleConfig>,
}

/// 控制台输出配置
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsoleConfig {
    /// 是否启用
    pub enabled: bool,
    /// 日志级别
    pub level: LogLevel,
    /// 是否彩色输出
    pub colored: bool,
}

impl Default for LoggerConfig {
    fn default() -> Self {
        Self {
            level: LogLevel::Info,
            dir: PathBuf::from("logs"),
            filename: "app.log".to_string(),
            rolling_policy: RollingPolicy::Time(TimeRollingConfig {
                period: TimePeriod::Daily,
                keep_days: 30,
            }),
            format: LogFormat::Text(TextFormatConfig {
                time_format: "%Y-%m-%d %H:%M:%S%.3f".to_string(),
                show_level: true,
                show_target: true,
                show_thread_id: true,
                show_file: true,
                show_line: true,
            }),
            async_write: true,
            split_by_level: false,
            console: Some(ConsoleConfig {
                enabled: true,
                level: LogLevel::Info,
                colored: true,
            }),
        }
    }
}