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
33
34
35
use schema_connector::ConnectorError;
use schema_core::TimingsLayer;
use tracing_error::ErrorLayer;

pub(crate) fn init_logger() {
    use tracing_subscriber::{prelude::*, EnvFilter, FmtSubscriber};

    let subscriber = FmtSubscriber::builder()
        .with_env_filter(EnvFilter::from_default_env())
        .json()
        .with_timer(tracing_subscriber::fmt::time::UtcTime::rfc_3339())
        .with_writer(std::io::stderr)
        .finish()
        .with(ErrorLayer::default())
        .with(TimingsLayer);

    tracing::subscriber::set_global_default(subscriber)
        .map_err(|err| eprintln!("Error initializing the global logger: {err}"))
        .ok();
}

pub(crate) fn log_error_and_exit(error: ConnectorError) -> ! {
    let message: &dyn std::fmt::Display = match error.known_error() {
        Some(known_error) => &known_error.message,
        _ => &error,
    };

    tracing::error!(
        is_panic = false,
        error_code = error.error_code().unwrap_or(""),
        message = %message,
    );

    std::process::exit(1)
}