baichun_framework_logger/
config.rs1use serde::{Deserialize, Serialize};
2use std::fmt;
3use std::path::PathBuf;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum LogLevel {
9 Error = 1,
11 Warn = 2,
13 Info = 3,
15 Debug = 4,
17 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#[derive(Debug, Clone, Serialize, Deserialize)]
47#[serde(rename_all = "lowercase")]
48pub enum RollingPolicy {
49 Time(TimeRollingConfig),
51 Size(SizeRollingConfig),
53 Compound {
55 time: TimeRollingConfig,
57 size: SizeRollingConfig,
59 },
60}
61
62#[derive(Debug, Clone, Serialize, Deserialize)]
64pub struct TimeRollingConfig {
65 pub period: TimePeriod,
67 pub keep_days: u32,
69}
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
73#[serde(rename_all = "lowercase")]
74pub enum TimePeriod {
75 Hourly,
77 Daily,
79 Weekly,
81 Monthly,
83}
84
85#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct SizeRollingConfig {
88 pub max_size: u64,
90 pub max_files: u32,
92}
93
94#[derive(Debug, Clone, Serialize, Deserialize)]
96#[serde(rename_all = "lowercase")]
97pub enum LogFormat {
98 Text(TextFormatConfig),
100 Json(JsonFormatConfig),
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct TextFormatConfig {
107 pub time_format: String,
109 pub show_level: bool,
111 pub show_target: bool,
113 pub show_thread_id: bool,
115 pub show_file: bool,
117 pub show_line: bool,
119}
120
121#[derive(Debug, Clone, Serialize, Deserialize)]
123pub struct JsonFormatConfig {
124 pub time_format: String,
126 pub pretty: bool,
128 pub include_caller: bool,
130 pub include_thread: bool,
132}
133
134#[derive(Debug, Clone, Serialize, Deserialize)]
136pub struct LoggerConfig {
137 pub level: LogLevel,
139 pub dir: PathBuf,
141 pub filename: String,
143 pub rolling_policy: RollingPolicy,
145 pub format: LogFormat,
147 pub async_write: bool,
149 pub split_by_level: bool,
151 pub console: Option<ConsoleConfig>,
153}
154
155#[derive(Debug, Clone, Serialize, Deserialize)]
157pub struct ConsoleConfig {
158 pub enabled: bool,
160 pub level: LogLevel,
162 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}