Skip to content

Commit 957e602

Browse files
lfranckeTechassi
andauthored
fix(stackable-telemetry): Disable ANSI colors when stdout is not a terminal (#1183)
* fix(stackable-telemetry): Disable ANSI colors when stdout is not a terminal * chore(telemetry): Add changelog entry --------- Co-authored-by: Techassi <git@techassi.dev>
1 parent 9620b45 commit 957e602

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

crates/stackable-telemetry/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
44

55
## [Unreleased]
66

7+
### Fixed
8+
9+
- Only use ANSI escape sequences if stdout is a terminal/tty. Piping the output to a file will now
10+
result in plain text log messages ([#1183]).
11+
12+
[#1183]: https://github.com/stackabletech/operator-rs/pull/1183
13+
714
## [0.6.2] - 2026-03-09
815

916
### Fixed

crates/stackable-telemetry/src/tracing/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//!
77
//! To get started, see [`Tracing`].
88
9-
use std::{ops::Not, path::PathBuf};
9+
use std::{io::IsTerminal, ops::Not, path::PathBuf};
1010

1111
use opentelemetry::trace::TracerProvider;
1212
use opentelemetry_appender_tracing::layer::OpenTelemetryTracingBridge;
@@ -432,15 +432,21 @@ impl Tracing {
432432

433433
// NOTE (@NickLarsenNZ): There is no elegant way to build the layer depending on formats because the types
434434
// returned from each subscriber "modifier" function is different (sometimes with different generics).
435+
// tracing-subscriber does not auto-detect whether stdout is a terminal
436+
// (https://github.com/tokio-rs/tracing/issues/1160), so we check explicitly.
437+
let use_ansi = std::io::stdout().is_terminal();
438+
435439
match log_format {
436440
Format::Plain => {
437-
let console_output_layer =
438-
tracing_subscriber::fmt::layer().with_filter(env_filter_layer);
441+
let console_output_layer = tracing_subscriber::fmt::layer()
442+
.with_ansi(use_ansi)
443+
.with_filter(env_filter_layer);
439444
layers.push(console_output_layer.boxed());
440445
}
441446
Format::Json => {
442447
let console_output_layer = tracing_subscriber::fmt::layer()
443448
.json()
449+
.with_ansi(use_ansi)
444450
.with_filter(env_filter_layer);
445451
layers.push(console_output_layer.boxed());
446452
}

0 commit comments

Comments
 (0)