Fix rolling file for logs

This commit is contained in:
Victor Mignot 2023-05-06 18:48:25 +02:00
parent 07005e60e1
commit b15c15a5db
No known key found for this signature in database
GPG key ID: FFE4EF056FB5E0D0
2 changed files with 15 additions and 36 deletions

View file

@ -1,3 +1,5 @@
use log::LevelFilter;
use log4rs::{ use log4rs::{
append::{ append::{
console::ConsoleAppender, console::ConsoleAppender,
@ -13,73 +15,50 @@ use log4rs::{
Config, Config,
}; };
const FILE_MAX_SIZE: u64 = 30000000; const FILE_MAX_SIZE: u64 = 30000000; //30 mB
const LOGS_MAX_FILES: u32 = 10; const LOGS_MAX_FILES: u32 = 10;
const DEBUG_MAX_FILES: u32 = 20;
const LOG_FORMAT: &str = "{d(%Y-%m-%d %H:%M:%S)} | {({l}):5.5} | {f}:{L} — {m}{n}"; const LOG_FORMAT: &str = "{d(%Y-%m-%d %H:%M:%S)} | {({l}):5.5} | {f}:{L} — {m}{n}";
const LOGS_FILE: &str = "logs/yorokobot_latest.log"; const LOGS_FILE: &str = "logs/yorokobot_latest.log";
const DEBUG_FILE: &str = "logs/debug/yorokobot_debug_latest.log";
const LOGS_ARCHIVE_FILE_PATTERN: &str = "logs/yorokobot_{}.gz"; const LOGS_ARCHIVE_FILE_PATTERN: &str = "logs/yorokobot_{}.gz";
const DEBUG_ARCHIVE_FILE_PATTERN: &str = "logs/debug/yorokobot_debug_{}.gz";
/// Configure the bot logger /// Configure the bot logger
pub fn init_logger() { pub fn init_logger() -> log4rs::Handle {
// Logs patterns
let encoder = PatternEncoder::new(LOG_FORMAT);
// Rollings logs trigger // Rollings logs trigger
let size_triger = SizeTrigger::new(FILE_MAX_SIZE); let size_triger = SizeTrigger::new(FILE_MAX_SIZE);
// Rolling logs roller // Rolling logs rollers
let logs_roller = FixedWindowRoller::builder() let logs_roller = FixedWindowRoller::builder()
.base(1)
.build(LOGS_ARCHIVE_FILE_PATTERN, LOGS_MAX_FILES) .build(LOGS_ARCHIVE_FILE_PATTERN, LOGS_MAX_FILES)
.unwrap(); .unwrap();
let rolling_debug = FixedWindowRoller::builder()
.build(DEBUG_ARCHIVE_FILE_PATTERN, DEBUG_MAX_FILES)
.unwrap();
// Rolling logs policy // Rolling logs policies
let rolling_logs_policy = CompoundPolicy::new(Box::new(size_triger), Box::new(logs_roller)); let rolling_logs_policy = CompoundPolicy::new(Box::new(size_triger), Box::new(logs_roller));
let rolling_debug_policy = CompoundPolicy::new(Box::new(size_triger), Box::new(rolling_debug));
// Appenders // Appenders
let stdout = ConsoleAppender::builder() let stdout = ConsoleAppender::builder()
.encoder(Box::new(encoder.clone())) .encoder(Box::new(PatternEncoder::new(LOG_FORMAT)))
.build(); .build();
let rolling_logs = RollingFileAppender::builder() let rolling_logs = RollingFileAppender::builder()
.encoder(Box::new(encoder.clone())) .encoder(Box::new(PatternEncoder::new(LOG_FORMAT)))
.build(LOGS_FILE, Box::new(rolling_logs_policy)) .build(LOGS_FILE, Box::new(rolling_logs_policy))
.unwrap(); .unwrap();
let rolling_debug = RollingFileAppender::builder()
.encoder(Box::new(encoder))
.build(DEBUG_FILE, Box::new(rolling_debug_policy))
.unwrap();
let config = Config::builder() let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout))) .appender(Appender::builder().build("stdout", Box::new(stdout)))
.appender(Appender::builder().build("rolling_logs", Box::new(rolling_logs))) .appender(Appender::builder().build("rolling_logs", Box::new(rolling_logs)))
.appender(Appender::builder().build("rolling_debug", Box::new(rolling_debug)))
// Don't print the serenity crate logs // Don't print the serenity crate logs
.logger(Logger::builder().build("serenity", log::LevelFilter::Error)) .logger(Logger::builder().build("serenity", LevelFilter::Error))
// Don't print the tracing crate logs // Don't print the tracing crate logs
.logger(Logger::builder().build("tracing", log::LevelFilter::Error)) .logger(Logger::builder().build("tracing", LevelFilter::Error))
.logger(
Logger::builder()
.appender("rolling_logs")
.build("logs", log::LevelFilter::Info),
)
.logger(
Logger::builder()
.appender("rolling_debug")
.build("bot_debug", log::LevelFilter::Debug),
)
.build( .build(
Root::builder() Root::builder()
.appender("stdout") .appender("stdout")
.build(log::LevelFilter::Info), .appender("rolling_logs")
.build(LevelFilter::Info),
) )
.unwrap(); .unwrap();
log4rs::init_config(config).unwrap(); log4rs::init_config(config).unwrap()
} }

View file

@ -18,7 +18,7 @@ use logs::init_logger;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// Start the logger // Start the logger
init_logger(); let _handle = init_logger();
let mut client = Client::new().await; let mut client = Client::new().await;