xmr-btc-swap/swap/src/asb/tracing.rs
rishflab 679b155db1 Enable log timestamps using explicit command line flag
Previously logs were only timestamped when the ASB was run in an
interactive terminal or if the logs were output as JSON. JSON logs and
ASB output in an interactive terminal are no longer timestamped by
default.
2021-09-09 21:42:07 +10:00

31 lines
949 B
Rust

use anyhow::Result;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::fmt::time::ChronoLocal;
use tracing_subscriber::FmtSubscriber;
pub fn init(level: LevelFilter, json_format: bool, timestamp: bool) -> Result<()> {
if level == LevelFilter::OFF {
return Ok(());
}
let is_terminal = atty::is(atty::Stream::Stderr);
let builder = FmtSubscriber::builder()
.with_env_filter(format!("asb={},swap={}", level, level))
.with_writer(std::io::stderr)
.with_ansi(is_terminal)
.with_timer(ChronoLocal::with_format("%F %T".to_owned()))
.with_target(false);
match (json_format, timestamp) {
(true, true) => builder.json().init(),
(true, false) => builder.json().without_time().init(),
(false, true) => builder.init(),
(false, false) => builder.without_time().init(),
}
tracing::info!(%level, "Initialized tracing");
Ok(())
}