Skip to content

SpanTrace with fmt layer includes ANSI codes in fields #3476

@ColonelThirtyTwo

Description

@ColonelThirtyTwo

Bug Report

Version

tracing v0.1.44
tracing-attributes v0.1.31
tracing-core v0.1.36
tracing-error v0.2.1
tracing-subscriber v0.3.22

Platform

Linux [hostname redacted] 6.18.7-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 24 Jan 2026 00:47:39 +0000 x86_64 GNU/Linux

Description

If the tracing registry contains a fmt layer that has ANSI colors turned on, the fields string returned by SpanTrace::with_spans will as well. This also includes SpanTrace's Display implementation.

Example code:

use tracing_subscriber::{layer::SubscriberExt as _, util::SubscriberInitExt as _};

fn main() {
    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer())
        .with(tracing_error::ErrorLayer::default())
        .init();

    let _guard = tracing::error_span!("test span", foo="bar").entered();
    let st = tracing_error::SpanTrace::capture();
    st.with_spans(|_md, fields| {
        println!("{:?}", fields);
        true
    });
    println!("{:?}", st.to_string());
}

Output:

"\u{1b}[3mfoo\u{1b}[0m\u{1b}[2m=\u{1b}[0m\"bar\""
"   0: test_tracing_error::test span\n           with \u{1b}[3mfoo\u{1b}[0m\u{1b}[2m=\u{1b}[0m\"bar\"\n             at src/main.rs:9"

Calling .with_ansi(false) on the fmt layer or removing it entirely results in plaintext output:

"foo=\"bar\""
"   0: test_tracing_error::test span\n           with foo=\"bar\"\n             at src/main.rs:9"

The documentation for with_spans doesn't mention that it emits ANSI escape codes. It makes reading fields in spantraces more difficult in places that don't support ANSI codes.

In particular, #3378 means that doing tracing::error("{:?}", color_eyre_error) will display ANSI escapes for the span fields, even if you turn color_eyre colors off.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions