logo
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//! Utilities for configuring the `env_logger` crate to emit `tracing` events.

/// Extension trait to configure an `env_logger::Builder` to emit traces.
pub trait BuilderExt: crate::sealed::Sealed {
    /// Configure the built `env_logger::Logger` to emit `tracing` events for
    /// all consumed `log` records, rather than printing them to standard out.
    ///
    /// Note that this replaces any previously configured formatting.
    fn emit_traces(&mut self) -> &mut Self;
}

impl crate::sealed::Sealed for env_logger::Builder {}

impl BuilderExt for env_logger::Builder {
    fn emit_traces(&mut self) -> &mut Self {
        self.format(|_, record| super::format_trace(record))
    }
}

/// Attempts to initialize the global logger with an env logger configured to
/// emit `tracing` events.
///
/// This should be called early in the execution of a Rust program. Any log
/// events that occur before initialization will be ignored.
///
/// # Errors
///
/// This function will fail if it is called more than once, or if another
/// library has already initialized a global logger.
pub fn try_init() -> Result<(), log::SetLoggerError> {
    env_logger::Builder::from_default_env()
        .emit_traces()
        .try_init()
}

/// Initializes the global logger with an env logger configured to
/// emit `tracing` events.
///
/// This should be called early in the execution of a Rust program. Any log
/// events that occur before initialization will be ignored.
///
/// # Panics
///
/// This function will panic if it is called more than once, or if another
/// library has already initialized a global logger.
pub fn init() {
    try_init()
        .expect("tracing_log::env_logger::init should not be called after logger initialized");
}