use crate::config::core::{LogLevel, LoggingConfig};
use crate::error::QuickDbError;
use rat_logger::info;
use std::path::PathBuf;
#[derive(Debug)]
pub struct LoggingConfigBuilder {
level: Option<LogLevel>,
console: Option<bool>,
file_path: Option<PathBuf>,
max_file_size: Option<u64>,
max_files: Option<u32>,
structured: Option<bool>,
}
impl LoggingConfigBuilder {
pub fn new() -> Self {
Self {
level: None,
console: None,
file_path: None,
max_file_size: None,
max_files: None,
structured: None,
}
}
pub fn level(mut self, level: LogLevel) -> Self {
self.level = Some(level);
self
}
pub fn console(mut self, console: bool) -> Self {
self.console = Some(console);
self
}
pub fn file_path<P: Into<PathBuf>>(mut self, file_path: Option<P>) -> Self {
self.file_path = file_path.map(|p| p.into());
self
}
pub fn max_file_size(mut self, max_file_size: u64) -> Self {
self.max_file_size = Some(max_file_size);
self
}
pub fn max_files(mut self, max_files: u32) -> Self {
self.max_files = Some(max_files);
self
}
pub fn structured(mut self, structured: bool) -> Self {
self.structured = Some(structured);
self
}
pub fn build(self) -> Result<LoggingConfig, QuickDbError> {
let level = self
.level
.ok_or_else(|| crate::quick_error!(config, "日志级别必须设置"))?;
let console = self
.console
.ok_or_else(|| crate::quick_error!(config, "控制台输出选项必须设置"))?;
let max_file_size = self
.max_file_size
.ok_or_else(|| crate::quick_error!(config, "日志文件最大大小必须设置"))?;
let max_files = self
.max_files
.ok_or_else(|| crate::quick_error!(config, "保留日志文件数量必须设置"))?;
let structured = self
.structured
.ok_or_else(|| crate::quick_error!(config, "结构化日志选项必须设置"))?;
if max_file_size == 0 {
return Err(crate::quick_error!(config, "日志文件最大大小不能为零"));
}
if max_files == 0 {
return Err(crate::quick_error!(config, "保留日志文件数量不能为零"));
}
info!(
"创建日志配置: 级别={:?}, 控制台={}, 结构化={}",
level, console, structured
);
Ok(LoggingConfig {
level,
console,
file_path: self.file_path,
max_file_size,
max_files,
structured,
})
}
}
impl Default for LoggingConfigBuilder {
fn default() -> Self {
Self::new()
}
}