binary_option_tools_core/utils/
tracing.rs

1use std::fs::OpenOptions;
2
3use tracing::level_filters::LevelFilter;
4use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt, Layer};
5
6pub fn start_tracing(terminal: bool) -> anyhow::Result<()> {
7    let error_logs = OpenOptions::new()
8        .append(true)
9        .create(true)
10        .open("errors.log")?;
11
12    let sub = tracing_subscriber::registry()
13        // .with(filtered_layer)
14        .with(
15            // log-error file, to log the errors that arise
16            fmt::layer()
17                .with_ansi(false)
18                .with_writer(error_logs)
19                .with_filter(LevelFilter::WARN),
20        );
21    if terminal {
22        sub.with(fmt::Layer::default().with_filter(LevelFilter::DEBUG))
23            .try_init()?;
24    } else {
25        sub.try_init()?;
26    }
27
28    Ok(())
29}