From 9820a5d3bc201e1789cf3b1dbbf83d9f77688175 Mon Sep 17 00:00:00 2001 From: Rayhaan Jaufeerally Date: Sat, 3 Oct 2026 15:12:43 +0200 Subject: [PATCH] Log without colour codes when the output is not a terminal Under systemd the log goes to the journal, where ANSI escape codes are noise. Colours are kept for a person at a terminal. Co-Authored-By: Claude Opus 5.5 --- bin/bgp/src/logging.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bin/bgp/src/logging.rs b/bin/bgp/src/logging.rs index 1af1279..8b0092c 100644 --- a/bin/bgp/src/logging.rs +++ b/bin/bgp/src/logging.rs @@ -1,5 +1,7 @@ //! What the daemon logs, and how much. +use std::io::IsTerminal; + use clap::Args; use tracing::Level; use tracing_subscriber::EnvFilter; @@ -23,7 +25,12 @@ impl LoggingArgs { let filter = EnvFilter::builder() .from_env_lossy() .add_directive(format!("{OURS}={}", self.level).parse()?); - tracing_subscriber::fmt().with_env_filter(filter).init(); + // Colours for a person at a terminal; plain text for the journal + // or a file, where escape codes are noise. + tracing_subscriber::fmt() + .with_env_filter(filter) + .with_ansi(std::io::stdout().is_terminal()) + .init(); Ok(()) } }