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
use tracing_error::ErrorLayer;
use tracing_subscriber::{
    fmt::{
        format::{DefaultFields, Format},
        TestWriter,
    },
    layer::Layered,
    prelude::*,
    EnvFilter, FmtSubscriber,
};

pub(crate) fn init_logger() {
    tracing::subscriber::set_global_default(test_tracing_subscriber())
        .map_err(|err| {
            eprintln!("Error initializing the global logger: {err}");
            std::process::exit(1);
        })
        .ok();
}

type Sub = Layered<
    ErrorLayer<FmtSubscriber<DefaultFields, Format, EnvFilter, TestWriter>>,
    FmtSubscriber<DefaultFields, Format, EnvFilter, TestWriter>,
>;

fn test_tracing_subscriber() -> Sub {
    FmtSubscriber::builder()
        .with_env_filter(EnvFilter::from_default_env())
        .with_test_writer()
        .finish()
        .with(ErrorLayer::default())
}