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
// SPDX-License-Identifier: AGPL-3.0-or-later

//! Helpers for configuring logging.

use chrono;
use env_logger;
use std::io::Write;

/// Extensions to [env_logger::Builder] which configure it for Blocktree crates.
pub trait BuilderExt {
    /// Uses a standard format for log messages which includes the source file and line number
    /// a logging statement occurs on.
    fn btformat(&mut self) -> &mut Self;
}

impl BuilderExt for env_logger::Builder {
    fn btformat(&mut self) -> &mut Self {
        self.format(|fmt, record| {
            writeln!(
                fmt,
                "[{} {} {}:{}] {}",
                chrono::Utc::now().to_rfc3339(),
                record.level(),
                record.file().unwrap_or("(unknown)"),
                record.line().unwrap_or(u32::MAX),
                record.args(),
            )
        })
    }
}

/// Initializes [env_logger] using the default environment and `btformat`.
pub fn init() {
    env_logger::Builder::from_default_env().btformat().init();
}