use crate::appender::{LogAppender, RecordFormat};
use crate::consts::LogSize;
use crate::filter::Filter;
use crate::plugin::console::{ConsoleAppender, ConsoleStderrAppender};
use crate::plugin::file::FileAppender;
use crate::plugin::file_loop::FileLoopAppender;
use crate::plugin::file_split::{
CanRollingPack, FileSplitAppender, Keep, Packer, RawFile, SplitFile,
};
use crate::FastLogFormat;
use dark_std::sync::SyncVec;
use log::LevelFilter;
use parking_lot::Mutex;
use std::fmt::{Debug, Formatter};
pub struct Config {
pub appends: SyncVec<Mutex<Box<dyn LogAppender>>>,
pub level: LevelFilter,
pub filters: SyncVec<Box<dyn Filter>>,
pub format: Box<dyn RecordFormat>,
pub chan_len: Option<usize>,
pub worker_tasks: Option<usize>,
}
impl Debug for Config {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Config")
.field("appends", &self.appends.len())
.field("level", &self.level)
.field("chan_len", &self.chan_len)
.finish()
}
}
impl Default for Config {
fn default() -> Self {
Self {
appends: SyncVec::new(),
level: LevelFilter::Trace,
filters: SyncVec::new(),
format: Box::new(FastLogFormat::new()),
chan_len: None,
worker_tasks: Some(1),
}
}
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn level(mut self, level: LevelFilter) -> Self {
self.level = level;
self
}
pub fn add_filter<F: Filter + 'static>(self, filter: F) -> Self {
self.filters.push(Box::new(filter));
self
}
pub fn filter(self, filters: Vec<Box<dyn Filter>>) -> Self {
for x in filters {
self.filters.push(x);
}
self
}
pub fn format<F: RecordFormat + 'static>(mut self, format: F) -> Self {
self.format = Box::new(format);
self
}
pub fn console(self) -> Self {
self.appends.push(Mutex::new(Box::new(ConsoleAppender {})));
self
}
pub fn console_stderr(self) -> Self {
self.appends.push(Mutex::new(Box::new(ConsoleStderrAppender {})));
self
}
pub fn file(self, file: &str) -> Self {
self.appends
.push(Mutex::new(Box::new(FileAppender::new(file).unwrap())));
self
}
pub fn file_loop(self, file: &str, max_temp_size: LogSize) -> Self {
self.appends.push(Mutex::new(Box::new(
FileLoopAppender::new(file, max_temp_size).expect("make file_loop fail"),
)));
self
}
pub fn file_split<
R: CanRollingPack + 'static,
K: Keep + 'static,
P: Packer + Sync + 'static,
>(
self,
file_path: &str,
rolling: R,
keeper: K,
packer: P,
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::new::<RawFile>(
file_path,
Box::new(rolling),
Box::new(keeper),
Box::new(packer),
)
.expect("new split file fail"),
)));
self
}
pub fn split<
F: SplitFile + 'static,
R: Keep + 'static,
P: Packer + Sync + 'static,
H: CanRollingPack + 'static,
>(
self,
file_path: &str,
keeper: R,
packer: P,
how_pack: H,
) -> Self {
self.appends.push(Mutex::new(Box::new(
FileSplitAppender::new::<F>(
file_path,
Box::new(how_pack),
Box::new(keeper),
Box::new(packer),
)
.expect("new split file fail"),
)));
self
}
pub fn custom<Appender: LogAppender + 'static>(self, arg: Appender) -> Self {
self.add_appender(arg)
}
pub fn add_appender<Appender: LogAppender + 'static>(self, arg: Appender) -> Self {
self.appends.push(Mutex::new(Box::new(arg)));
self
}
pub fn chan_len(mut self, len: Option<usize>) -> Self {
self.chan_len = len;
self
}
pub fn worker_tasks(mut self, num: Option<usize>) -> Self {
self.worker_tasks = num;
self
}
}