Skip to content

subscriber: allow ansi sanitization to be disabled#3484

Merged
hds merged 4 commits intotokio-rs:mainfrom
Lur1an:feat/configurable-ansi-sanitization
Mar 12, 2026
Merged

subscriber: allow ansi sanitization to be disabled#3484
hds merged 4 commits intotokio-rs:mainfrom
Lur1an:feat/configurable-ansi-sanitization

Conversation

@Lur1an
Copy link
Contributor

@Lur1an Lur1an commented Mar 6, 2026

Fix: #3369

Add ansi_sanitization flag to allow turning off the sanitization of ansi output.
Sanitizing output remains the default but can now be turned off.

Solution

ansi_sanitization is passed into the new AnsiValue struct which now replaces Escape together with a value: T, when enabled will escape values with the same logic as Escape relying on EscapingWriter.

/// A wrapper that conditionally escapes ANSI sequences when formatted.
pub(super) struct AnsiValue<T> {
    pub(super) value: T,
    pub(super) sanitize: bool,
}

impl<T> AnsiValue<T> {
    pub(super) fn new(value: T, sanitize: bool) -> Self {
        Self { value, sanitize }
    }
}

impl<T: fmt::Debug> fmt::Debug for AnsiValue<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.sanitize {
            let mut escaping_writer = EscapingWriter { inner: f };
            write!(escaping_writer, "{:?}", self.value)
        } else {
            write!(f, "{:?}", self.value)
        }
    }
}

impl<T: fmt::Display> fmt::Display for AnsiValue<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.sanitize {
            let mut escaping_writer = EscapingWriter { inner: f };
            write!(escaping_writer, "{}", self.value)
        } else {
            write!(f, "{}", self.value)
        }
    }
}

@Lur1an Lur1an requested review from a team, hawkw and hds as code owners March 6, 2026 16:27
Copy link
Contributor

@hds hds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking in this PR! It's really appreciated!

I've got a naming question and perhaps another test needed?

@Lur1an Lur1an force-pushed the feat/configurable-ansi-sanitization branch from 6a39abc to 758c1cc Compare March 8, 2026 22:55
@Lur1an Lur1an force-pushed the feat/configurable-ansi-sanitization branch from 921dc05 to 55cce37 Compare March 11, 2026 13:38
Copy link
Contributor

@hds hds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your change!

@hds hds changed the title feature: add configurable ansi sanitization via `with_ansi_sanitizati… subscriber: allow ansi sanitization to be disabled Mar 12, 2026
@hds hds merged commit 37558d5 into tokio-rs:main Mar 12, 2026
55 checks passed
@Naxdy Naxdy mentioned this pull request Mar 12, 2026
hds added a commit that referenced this pull request Mar 12, 2026
# 0.3.23 (March 13, 2026)

### Fixed

- Allow ansi sanitization to be disabled ([#3484])

[#3484]: #3484
hds added a commit that referenced this pull request Mar 13, 2026
# 0.3.23 (March 13, 2026)

### Fixed

- Allow ansi sanitization to be disabled ([#3484])

[#3484]: #3484
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Regression: New tracing-subscriber breaks ANSI color and styling support

2 participants