2023-03-26 21:49:34 +02:00
|
|
|
use crate::config::Config;
|
2023-03-21 13:00:44 +01:00
|
|
|
use env_logger::{Builder, Env, Target};
|
2023-03-26 21:49:34 +02:00
|
|
|
use log::LevelFilter;
|
2023-03-23 14:57:24 +01:00
|
|
|
use std::io::Write;
|
2023-03-21 13:00:44 +01:00
|
|
|
|
2023-03-26 21:49:34 +02:00
|
|
|
pub fn init_log_system(cnf: &Config) {
|
2023-03-23 14:57:24 +01:00
|
|
|
let env = Env::new().filter_or(crate::LOG_LEVEL_ENV_VAR, "warn");
|
2023-03-21 13:00:44 +01:00
|
|
|
let mut builder = Builder::from_env(env);
|
2023-03-23 14:57:24 +01:00
|
|
|
builder.format(|buf, record| {
|
|
|
|
writeln!(
|
|
|
|
buf,
|
|
|
|
"{}: {}",
|
|
|
|
record.level().to_string().to_lowercase(),
|
|
|
|
record.args()
|
|
|
|
)
|
|
|
|
});
|
2023-03-21 13:00:44 +01:00
|
|
|
builder.target(Target::Stderr);
|
2023-03-26 21:49:34 +02:00
|
|
|
builder.filter_level(cnf.verbosity());
|
2023-03-21 13:00:44 +01:00
|
|
|
builder.init();
|
|
|
|
}
|
2023-03-26 21:49:34 +02:00
|
|
|
|
|
|
|
pub fn log_level(level_nb: u8) -> LevelFilter {
|
|
|
|
match level_nb {
|
|
|
|
0 => LevelFilter::Info,
|
|
|
|
1 => LevelFilter::Debug,
|
|
|
|
_ => LevelFilter::Trace,
|
|
|
|
}
|
|
|
|
}
|