2021-03-02 19:51:33 +00:00
|
|
|
use crate::infra::configuration::Configuration;
|
|
|
|
use anyhow::Context;
|
|
|
|
use tracing::subscriber::set_global_default;
|
|
|
|
use tracing_log::LogTracer;
|
|
|
|
|
2021-03-02 20:43:26 +00:00
|
|
|
pub fn init(config: Configuration) -> anyhow::Result<()> {
|
|
|
|
let max_log_level = log_level_from_config(config);
|
2021-03-02 19:51:33 +00:00
|
|
|
let subscriber = tracing_subscriber::fmt()
|
|
|
|
.with_timer(tracing_subscriber::fmt::time::time())
|
|
|
|
.with_target(false)
|
|
|
|
.with_level(true)
|
2021-03-02 20:43:26 +00:00
|
|
|
.with_max_level(max_log_level)
|
2021-03-02 19:51:33 +00:00
|
|
|
.finish();
|
|
|
|
LogTracer::init().context("Failed to set logger")?;
|
|
|
|
set_global_default(subscriber).context("Failed to set subscriber")?;
|
2021-11-03 07:01:34 +00:00
|
|
|
// TODO: Only log SQL statements >= warn unless verbose.
|
2021-03-02 19:51:33 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-02 20:43:26 +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
|
|
|
|
}
|
|
|
|
}
|