Disable timestamp if we log to a non-interactive terminal

A non-interactive terminal is likely something along the lines of
journalctl which captures a timestamp by itself. In theory, it could
also be just a logfile but we rather accept this limitation and keep
the configuration surface simple rather than exposing another config
switch.
This commit is contained in:
Thomas Eizinger 2021-03-17 11:41:28 +11:00
parent edb8851ce2
commit 9e3a104b42
No known key found for this signature in database
GPG Key ID: 651AC83A6C6C8B96

View File

@ -1,5 +1,4 @@
use anyhow::Result;
use tracing::{info, subscriber};
use tracing_log::LogTracer;
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::FmtSubscriber;
@ -13,14 +12,20 @@ pub fn init_tracing(level: LevelFilter) -> Result<()> {
LogTracer::init_with_filter(tracing_log::log::LevelFilter::Info)?;
let is_terminal = atty::is(atty::Stream::Stderr);
let subscriber = FmtSubscriber::builder()
.with_env_filter(format!("asb={},swap={}", level, level,))
let builder = FmtSubscriber::builder()
.with_env_filter(format!("asb={},swap={}", level, level))
.with_writer(std::io::stderr)
.with_ansi(is_terminal)
.finish();
.with_target(false);
subscriber::set_global_default(subscriber)?;
info!("Initialized tracing with level: {}", level);
if !is_terminal {
builder.without_time().init();
} else {
builder.init();
}
tracing::info!("Initialized tracing with level: {}", level);
Ok(())
}