rwf/
logging.rs

1//! Wrapper around `tracing_subscriber` for logging.
2//!
3//! Configures application-wide logging to go to stderr at the `INFO` level.
4//! If you prefer to use your own logging subscriber, don't initialize the `Logger`.
5//!
6//! ### Example
7//!
8//! ```rust
9//! use rwf::prelude::*;
10//!
11//! Logger::init();
12//! ```
13use crate::config::get_config;
14use once_cell::sync::OnceCell;
15use tracing_subscriber::{filter::LevelFilter, fmt, util::SubscriberInitExt, EnvFilter};
16
17static INITIALIZED: OnceCell<()> = OnceCell::new();
18
19pub struct Logger;
20
21impl Logger {
22    /// Configure logging application-wide.
23    ///
24    /// Calling this multiple times is safe. Logger will be initialized only once.
25    pub fn init() {
26        INITIALIZED.get_or_init(|| {
27            setup_logging();
28            get_config().log_info();
29
30            ()
31        });
32    }
33}
34
35fn setup_logging() {
36    fmt()
37        .with_env_filter(
38            EnvFilter::builder()
39                .with_default_directive(LevelFilter::INFO.into())
40                .from_env_lossy(),
41        )
42        .with_ansi(get_config().general.tty)
43        .with_file(false)
44        .with_target(false)
45        .finish()
46        .init();
47}