2021-03-02 19:51:33 +00:00
|
|
|
use crate::infra::configuration::Configuration;
|
2021-11-14 00:02:48 +00:00
|
|
|
use tracing_subscriber::prelude::*;
|
2021-03-02 19:51:33 +00:00
|
|
|
|
2021-11-11 09:14:03 +00:00
|
|
|
pub fn init(config: &Configuration) -> anyhow::Result<()> {
|
2021-03-02 20:43:26 +00:00
|
|
|
let max_log_level = log_level_from_config(config);
|
2021-11-14 00:02:48 +00:00
|
|
|
let sqlx_max_log_level = sqlx_log_level_from_config(config);
|
|
|
|
let filter = tracing_subscriber::filter::Targets::new()
|
|
|
|
.with_target("lldap", max_log_level)
|
|
|
|
.with_target("sqlx", sqlx_max_log_level);
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
.with(tracing_subscriber::fmt::layer().with_filter(filter))
|
|
|
|
.init();
|
2021-03-02 19:51:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-02 20:43:26 +00:00
|
|
|
|
2021-11-11 09:14:03 +00:00
|
|
|
fn log_level_from_config(config: &Configuration) -> tracing::Level {
|
2021-03-02 21:03:58 +00:00
|
|
|
if config.verbose {
|
2021-03-02 20:43:26 +00:00
|
|
|
tracing::Level::DEBUG
|
|
|
|
} else {
|
|
|
|
tracing::Level::INFO
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 00:02:48 +00:00
|
|
|
|
|
|
|
fn sqlx_log_level_from_config(config: &Configuration) -> tracing::Level {
|
|
|
|
if config.verbose {
|
|
|
|
tracing::Level::INFO
|
|
|
|
} else {
|
|
|
|
tracing::Level::WARN
|
|
|
|
}
|
|
|
|
}
|