JHHM

lol-cal

terminal lolesports schedule viewer
git clone https://jhhm.no/repos/lol-cal.git

lol-cal/src/logging.rs

get raw file: logging.rs

1use color_eyre::Result;
2use tracing_error::ErrorLayer;
3use tracing_subscriber::{EnvFilter, fmt, prelude::*};
4
5use crate::config;
6
7lazy_static::lazy_static! {
8    pub static ref LOG_ENV: String = format!("{}_LOG_LEVEL", config::PROJECT_NAME.clone());
9    pub static ref LOG_FILE: String = format!("{}.log", env!("CARGO_PKG_NAME"));
10}
11
12pub fn init() -> Result<()> {
13    let directory = config::utils::get_data_dir();
14    std::fs::create_dir_all(directory.clone())?;
15    let log_path = directory.join(LOG_FILE.clone());
16    let log_file = std::fs::File::create(log_path)?;
17    let env_filter = EnvFilter::builder().with_default_directive(tracing::Level::INFO.into());
18    // If the `RUST_LOG` environment variable is set, use that as the default, otherwise use the
19    // value of the `LOG_ENV` environment variable. If the `LOG_ENV` environment variable contains
20    // errors, then this will return an error.
21    let env_filter = env_filter
22        .try_from_env()
23        .or_else(|_| env_filter.with_env_var(LOG_ENV.clone()).from_env())?;
24    let file_subscriber = fmt::layer()
25        .with_file(true)
26        .with_line_number(true)
27        .with_writer(log_file)
28        .with_target(false)
29        .with_ansi(false)
30        .with_filter(env_filter);
31    tracing_subscriber::registry()
32        .with(file_subscriber)
33        .with(ErrorLayer::default())
34        .try_init()?;
35    Ok(())
36}