1
use color_eyre::{
2
    eyre::{eyre, Context},
3
    Result,
4
};
5
use env_logger::{Builder, Target};
6
use log::LevelFilter;
7

            
8
#[allow(clippy::comparison_chain)]
9
/// [`config_logger`] The configuration for `env_logger`
10
pub fn config_logger(verbose_level: u8, target: Target) -> Result<()> {
11
    let mut builder = Builder::from_default_env();
12

            
13
    builder
14
        .target(target)
15
        .default_format()
16
        .format_indent(Some(4))
17
        .format_module_path(false)
18
        .format_timestamp_millis()
19
        .write_style(env_logger::WriteStyle::Always);
20

            
21
    if verbose_level == 1 {
22
        builder.filter(None, LevelFilter::Debug);
23
    } else if verbose_level == 2 {
24
        builder.filter(None, LevelFilter::Trace);
25
    } else if verbose_level > 2 {
26
        return Err(eyre!("Zork++ maximum allowed verbosity level is: '-vv'"));
27
    } else {
28
        builder.filter(None, LevelFilter::Info);
29
    }
30

            
31
    builder
32
        .try_init()
33
        .with_context(|| "Zork++ wasn't unable to set up the logger")
34
}