From 9e3a104b42a3d70e97796b81c1840bbf6de39ddc Mon Sep 17 00:00:00 2001 From: Thomas Eizinger Date: Wed, 17 Mar 2021 11:41:28 +1100 Subject: [PATCH] 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. --- swap/src/trace.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/swap/src/trace.rs b/swap/src/trace.rs index 253ca792..2c80de97 100644 --- a/swap/src/trace.rs +++ b/swap/src/trace.rs @@ -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(()) }