baichun_framework_logger/
config.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3use std::path::PathBuf;
4
5/// 日志级别
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum LogLevel {
9    /// 错误级别
10    Error = 1,
11    /// 警告级别
12    Warn = 2,
13    /// 信息级别
14    Info = 3,
15    /// 调试级别
16    Debug = 4,
17    /// 追踪级别
18    Trace = 5,
19}
20
21impl From<LogLevel> for tracing::Level {
22    fn from(level: LogLevel) -> Self {
23        match level {
24            LogLevel::Error => tracing::Level::ERROR,
25            LogLevel::Warn => tracing::Level::WARN,
26            LogLevel::Info => tracing::Level::INFO,
27            LogLevel::Debug => tracing::Level::DEBUG,
28            LogLevel::Trace => tracing::Level::TRACE,
29        }
30    }
31}
32
33impl fmt::Display for LogLevel {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            LogLevel::Error => write!(f, "ERROR"),
37            LogLevel::Warn => write!(f, "WARN"),
38            LogLevel::Info => write!(f, "INFO"),
39            LogLevel::Debug => write!(f, "DEBUG"),
40            LogLevel::Trace => write!(f, "TRACE"),
41        }
42    }
43}
44
45/// 日志滚动策略
46#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(rename_all = "lowercase")]
48pub enum RollingPolicy {
49    /// 按时间滚动
50    Time(TimeRollingConfig),
51    /// 按大小滚动
52    Size(SizeRollingConfig),
53    /// 混合滚动策略
54    Compound {
55        /// 时间滚动配置
56        time: TimeRollingConfig,
57        /// 大小滚动配置
58        size: SizeRollingConfig,
59    },
60}
61
62/// 时间滚动配置
63#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct TimeRollingConfig {
65    /// 滚动周期
66    pub period: TimePeriod,
67    /// 保留天数
68    pub keep_days: u32,
69}
70
71/// 时间周期
72#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(rename_all = "lowercase")]
74pub enum TimePeriod {
75    /// 每小时
76    Hourly,
77    /// 每天
78    Daily,
79    /// 每周
80    Weekly,
81    /// 每月
82    Monthly,
83}
84
85/// 大小滚动配置
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct SizeRollingConfig {
88    /// 最大文件大小(字节)
89    pub max_size: u64,
90    /// 最大文件数
91    pub max_files: u32,
92}
93
94/// 日志格式
95#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(rename_all = "lowercase")]
97pub enum LogFormat {
98    /// 文本格式
99    Text(TextFormatConfig),
100    /// JSON格式
101    Json(JsonFormatConfig),
102}
103
104/// 文本格式配置
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct TextFormatConfig {
107    /// 时间格式
108    pub time_format: String,
109    /// 是否显示级别
110    pub show_level: bool,
111    /// 是否显示目标
112    pub show_target: bool,
113    /// 是否显示线程ID
114    pub show_thread_id: bool,
115    /// 是否显示文件名
116    pub show_file: bool,
117    /// 是否显示行号
118    pub show_line: bool,
119}
120
121/// JSON格式配置
122#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct JsonFormatConfig {
124    /// 时间格式
125    pub time_format: String,
126    /// 是否美化输出
127    pub pretty: bool,
128    /// 是否包含调用者信息
129    pub include_caller: bool,
130    /// 是否包含线程信息
131    pub include_thread: bool,
132}
133
134/// 日志配置
135#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct LoggerConfig {
137    /// 日志级别
138    pub level: LogLevel,
139    /// 日志目录
140    pub dir: PathBuf,
141    /// 日志文件名
142    pub filename: String,
143    /// 滚动策略
144    pub rolling_policy: RollingPolicy,
145    /// 日志格式
146    pub format: LogFormat,
147    /// 是否异步写入
148    pub async_write: bool,
149    /// 是否按级别分离文件
150    pub split_by_level: bool,
151    /// 控制台输出配置
152    pub console: Option<ConsoleConfig>,
153}
154
155/// 控制台输出配置
156#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct ConsoleConfig {
158    /// 是否启用
159    pub enabled: bool,
160    /// 日志级别
161    pub level: LogLevel,
162    /// 是否彩色输出
163    pub colored: bool,
164}
165
166impl Default for LoggerConfig {
167    fn default() -> Self {
168        Self {
169            level: LogLevel::Info,
170            dir: PathBuf::from("logs"),
171            filename: "app.log".to_string(),
172            rolling_policy: RollingPolicy::Time(TimeRollingConfig {
173                period: TimePeriod::Daily,
174                keep_days: 30,
175            }),
176            format: LogFormat::Text(TextFormatConfig {
177                time_format: "%Y-%m-%d %H:%M:%S%.3f".to_string(),
178                show_level: true,
179                show_target: true,
180                show_thread_id: true,
181                show_file: true,
182                show_line: true,
183            }),
184            async_write: true,
185            split_by_level: false,
186            console: Some(ConsoleConfig {
187                enabled: true,
188                level: LogLevel::Info,
189                colored: true,
190            }),
191        }
192    }
193}