use std::fs::OpenOptions;
use slog::{o, Drain, Logger};
use slog_async::OverflowStrategy;
use crate::conf::LoggingFormat;
pub(crate) fn terminal_logger() -> Logger {
let decorator = slog_term::TermDecorator::new().build();
let drain = slog_term::FullFormat::new(decorator)
.use_file_location()
.build()
.fuse();
let drain = slog_async::Async::new(drain)
.overflow_strategy(OverflowStrategy::Block)
.chan_size(1)
.build()
.fuse();
Logger::root(drain, o!())
}
fn json_logger(filepath: &str) -> Logger {
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(true)
.open(filepath)
.unwrap();
let decorator = slog_json::Json::default(file).fuse();
let drain = slog_async::Async::new(decorator).build().fuse();
Logger::root(drain, o!())
}
pub(crate) fn logger(logging_format: &LoggingFormat) -> Logger {
match logging_format {
LoggingFormat::Terminal => terminal_logger(),
LoggingFormat::Json => json_logger("logs/rocketmq_client.log"),
}
}