Config logging from code instead of yaml

This commit is contained in:
Victor Mignot 2023-04-30 23:20:34 +02:00
parent 96cd51c5ce
commit 058e682043
No known key found for this signature in database
GPG key ID: FFE4EF056FB5E0D0
3 changed files with 88 additions and 61 deletions

View file

@ -1,60 +0,0 @@
refresh_rate: 30 seconds
appenders:
stdout:
kind: console
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S)} | {({l}):5.5} | {f}:{L} — {m}{n}"
rolling_logs:
kind: rolling_file
path: logs/yorokobot-latest.log
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S)} | {({l}):5.5} | {f}:{L} — {m}{n}"
policy:
trigger: # Specify when to switch to another log file
kind: size
limit: 30 mb
roller: # Specify what to do with older logs files
kind: fixed_window
pattern: logs/yorokobot_{}.gz
count: 10
rolling_debug:
kind: rolling_file
path: logs/traces-latest.log
encoder:
pattern: "{d(%Y-%m-%d %H:%M:%S)} | {({l}):5.5} | {f}:{L} — {m}{n}"
policy:
trigger:
kind: size
limit: 30 mb
roller:
kind: fixed_window
pattern: logs/yorokobot_traces_{}.gz
count: 20
root:
level: info
appenders:
- stdout
loggers:
serenity:
level: error
tracing:
level: error
logs:
level: info
appenders:
- rolling_logs
bot_traces:
level: debug
appenders:
- rolling_debug

85
src/logs.rs Normal file
View file

@ -0,0 +1,85 @@
use log4rs::{
append::{
console::ConsoleAppender,
rolling_file::{
policy::compound::{
roll::fixed_window::FixedWindowRoller, trigger::size::SizeTrigger, CompoundPolicy,
},
RollingFileAppender,
},
},
config::{Appender, Logger, Root},
encode::pattern::PatternEncoder,
Config,
};
const FILE_MAX_SIZE: u64 = 30000000;
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 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 DEBUG_ARCHIVE_FILE_PATTERN: &str = "logs/debug/yorokobot_debug_{}.gz";
/// Configure the bot logger
pub fn init_logger() {
// Logs patterns
let encoder = PatternEncoder::new(LOG_FORMAT);
// Rollings logs trigger
let size_triger = SizeTrigger::new(FILE_MAX_SIZE);
// Rolling logs roller
let logs_roller = FixedWindowRoller::builder()
.build(LOGS_ARCHIVE_FILE_PATTERN, LOGS_MAX_FILES)
.unwrap();
let rolling_debug = FixedWindowRoller::builder()
.build(DEBUG_ARCHIVE_FILE_PATTERN, DEBUG_MAX_FILES)
.unwrap();
// Rolling logs policy
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
let stdout = ConsoleAppender::builder()
.encoder(Box::new(encoder.clone()))
.build();
let rolling_logs = RollingFileAppender::builder()
.encoder(Box::new(encoder.clone()))
.build(LOGS_FILE, Box::new(rolling_logs_policy))
.unwrap();
let rolling_debug = RollingFileAppender::builder()
.encoder(Box::new(encoder))
.build(DEBUG_FILE, Box::new(rolling_debug_policy))
.unwrap();
let config = Config::builder()
.appender(Appender::builder().build("stdout", Box::new(stdout)))
.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
.logger(Logger::builder().build("serenity", log::LevelFilter::Error))
// Don't print the tracing crate logs
.logger(Logger::builder().build("tracing", log::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(
Root::builder()
.appender("stdout")
.build(log::LevelFilter::Info),
)
.unwrap();
log4rs::init_config(config).unwrap();
}

View file

@ -10,13 +10,15 @@ mod client;
mod database; mod database;
mod discord; mod discord;
mod environment; mod environment;
mod logs;
use client::Client; use client::Client;
use logs::init_logger;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
// Start the logger // Start the logger
log4rs::init_file("log4rs_config.yaml", Default::default()).unwrap(); init_logger();
let mut client = Client::new().await; let mut client = Client::new().await;