use std::{fs::OpenOptions, path::PathBuf};
use tracing::level_filters::LevelFilter;
use tracing_subscriber::{EnvFilter, fmt::format::FmtSpan};
fn log_level(is_debug: bool) -> LevelFilter {
match is_debug {
true => LevelFilter::DEBUG,
false => LevelFilter::INFO,
}
}
pub(crate) fn init_logging_stderr(
is_debug: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
tracing_subscriber::fmt()
.with_max_level(log_level(is_debug))
.with_span_events(FmtSpan::CLOSE)
.with_writer(std::io::stderr)
.with_ansi(true)
.try_init()
}
pub(crate) fn init_logging_file(
is_debug: bool,
path: &PathBuf,
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let filter = EnvFilter::try_from_default_env().unwrap_or(
EnvFilter::builder()
.with_default_directive(log_level(is_debug).into())
.from_env_lossy(),
);
if let Err(e) = tracing_subscriber::fmt()
.with_env_filter(filter)
.with_span_events(FmtSpan::CLOSE)
.with_writer(OpenOptions::new().append(true).create(true).open(path)?)
.try_init()
{
eprintln!("Failed to initialize logging: {e}");
}
Ok(())
}