xmr-btc-swap/swap/src/asb/tracing.rs

31 lines
920 B
Rust
Raw Normal View History

use anyhow::Result;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::fmt::time::UtcTime;
use tracing_subscriber::FmtSubscriber;
pub fn init(level: LevelFilter, json_format: bool, timestamp: bool) -> Result<()> {
if level == LevelFilter::OFF {
return Ok(());
}
2020-12-04 05:27:17 +00:00
let is_terminal = atty::is(atty::Stream::Stderr);
let builder = FmtSubscriber::builder()
.with_env_filter(format!("asb={},swap={}", level, level))
2020-12-04 05:27:17 +00:00
.with_writer(std::io::stderr)
.with_ansi(is_terminal)
.with_timer(UtcTime::rfc_3339())
.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(())
}