Skip to content

Commit

Permalink
subscriber: update pretty formatter for no ansi (tokio-rs#1240)
Browse files Browse the repository at this point in the history
This backports tokio-rs#1240 from `master`.

* subscriber: update pretty formatter for no ansi

 ## Background

    Currently, when the `Pretty` event formatter is being used, it does
    not change its output when the `with_ansi` flag is set to false by
    the `CollectorBuilder`.

 ## Overview

    While this formatter is generally used in situations such as local
    development, where ANSI escape codes are more often acceptable,
    there are some situations in which this can lead to mangled output.

    This commit makes some minor changes to account for this `ansi` flag
    when formatting events using `Pretty`.

    Becuase ANSI codes were previously used to imply the event level
    using colors, this commit additionally modifies `Pretty` so that
    it respects `display_level` when formatting an event.

 ## Changes

    * Changes to `<Format<Pretty, T> as FormatEvent<C, N>>::format_event`

    * Add a `PrettyVisitor::ansi` boolean flag, used in its `Visit`
      implementation.

        * Add a new `PrettyVisitor::with_ansi` builder pattern method to
          facilitate this.

 ## Out of Scope

    One detail worth nothing is that this does not solve the problem of
    *fields* being formatted without ANSI codes. Configuring a
    subscriber using this snippet would still lead to bolded fields in
    parent spans.

```rust
tracing_subscriber::fmt()
    .pretty()
    .with_ansi(false)
    .with_level(false)
    .with_max_level(tracing::Level::TRACE)
    .init();
```

    This can be worked around by using a different field formatter, via
    `.fmt_fields(tracing_subscriber::fmt::format::DefaultFields::new())`
    in the short-term.

    In the long-term, tokio-rs#658 is worth investigating further.

Refs: tokio-rs#658
  • Loading branch information
katelyn martin authored and kaffarell committed May 22, 2024
1 parent cde6396 commit 1f73dd4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tracing-subscriber/src/fmt/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,23 @@ impl<F, T> Format<F, T> {
/// See [`Pretty`].
///
/// Note that this requires the "ansi" feature to be enabled.
///
/// # Options
///
/// [`Format::with_ansi`] can be used to disable ANSI terminal escape codes (which enable
/// formatting such as colors, bold, italic, etc) in event formatting. However, a field
/// formatter must be manually provided to avoid ANSI in the formatting of parent spans, like
/// so:
///
/// ```
/// # use tracing_subscriber::fmt::format;
/// tracing_subscriber::fmt()
/// .pretty()
/// .with_ansi(false)
/// .fmt_fields(format::PrettyFields::new().with_ansi(false))
/// // ... other settings ...
/// .init();
/// ```
#[cfg(feature = "ansi")]
#[cfg_attr(docsrs, doc(cfg(feature = "ansi")))]
pub fn pretty(self) -> Format<Pretty, T> {
Expand Down
6 changes: 6 additions & 0 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ pub struct Pretty {
pub struct PrettyVisitor<'a> {
writer: Writer<'a>,
is_empty: bool,
ansi: bool,
style: Style,
result: fmt::Result,
}
Expand Down Expand Up @@ -397,6 +398,7 @@ impl<'a> PrettyVisitor<'a> {
Self {
writer,
is_empty,
ansi: true,
style: Style::default(),
result: Ok(()),
}
Expand All @@ -406,6 +408,10 @@ impl<'a> PrettyVisitor<'a> {
Self { style, ..self }
}

pub(crate) fn with_ansi(self, ansi: bool) -> Self {
Self { ansi, ..self }
}

fn write_padded(&mut self, value: &impl fmt::Debug) {
let padding = if self.is_empty {
self.is_empty = false;
Expand Down

0 comments on commit 1f73dd4

Please sign in to comment.