// 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();
}