aboutsummaryrefslogtreecommitdiffstats
path: root/src/logging.rs
blob: c97948f58144f3d3a318415bbb5bae1a497b753a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use crate::Result;
use figment::Provider;
use std::{fs::File, sync::Arc};
use tracing::metadata::LevelFilter;
use tracing_subscriber::{prelude::*, Layer};

pub use config::Config;
pub use level::Level;

mod config;
mod level;

pub fn init_from_provider<T: Provider>(provider: T) -> Result<()> {
    let config = Config::extract(&provider)?;

    let stdout_layer = tracing_subscriber::fmt::layer()
        .pretty()
        .with_filter(LevelFilter::from(config.level));

    let log_layer = if config.level.is_some() {
        let file = File::create(&config.path)?;
        tracing_subscriber::fmt::layer()
            .with_writer(Arc::new(file))
            .with_filter(LevelFilter::from(config.level))
            .into()
    } else {
        None
    };

    tracing_subscriber::registry()
        .with(stdout_layer)
        .with(log_layer)
        .init();

    Ok(())
}