tracing icon indicating copy to clipboard operation
tracing copied to clipboard

Only colorize output when outputting to a TTY

Open olix0r opened this issue 3 years ago • 6 comments

Feature Request

Motivation

Tracing's default output is colorized. But when a program is not connected to a terminal, for instance because its output is redirected to a file or another program like grep, this is undesirable because ANSI characters pollute the output.

Proposal

By default, tracing should only colorize output when its output stream is a TTY, similarly to how cargo works. When the output does not target a terminal, ANSI characters should be omitted.

It's probably desirable to support an environment variable, e.g. RUST_LOG_COLOR, that supports the values auto (default), never, and always (similar to cargo --color=...) so that users may override the automatic behavior as desired. This is especially useful when, for instance, redirecting the program's output like |& less so that colorized output can still be viewed in the pager.

olix0r avatar Dec 29 '20 19:12 olix0r

It's probably desirable to support an environment variable, e.g. RUST_LOG_COLOR

Rustc does this with an environment variable, RUSTC_LOG_COLOR. I would prefer not to do this in tracing itself so the variable can be customized. Tracing could add an API setting the variable but at that point you may as well do it yourself in the application.

jyn514 avatar Jan 03 '21 22:01 jyn514

An env variable can be implemented in user code with the current APIs in tracing-subscriber fmt::Layer::with_ansi and fmt::SubscriberBuilder::with_ansi. (Sidenote: maybe these should be renamed to with_colors or something in 0.3 for clarity?)

We could add support for a default env variable, though, if that's something people want --- but that part should be pretty easy to implement on top of the current API.

It's automatically detecting whether the output is a TTY that's significantly more challenging, especially with the current design of tracing_subscriber::fmt, where formatters are decoupled from IO. It's the IO writer that would know if the output supports ANSI colors, but the decision of whether or not to use ANSI colors is currently up to the formatter. We might have to redesign this a bit in order to support automatic detection.

hawkw avatar Jan 04 '21 18:01 hawkw

I would request that with_ansi be renamed to with_colors. Was confused as to how to disable colors and ansi was never the word I looked for in docs

coderedart avatar Jan 23 '22 12:01 coderedart

We could rename it to with_ansi_colors or something, in v0.4. I do think it's also important to be clear that the method specifically enables ANSI formatting escape codes, and not other forms of coloring, but I agree that it could be clearer if the name had the word "color" in it.

It does also control other formatting, though, such as bold and italic..

hawkw avatar Jan 24 '22 21:01 hawkw

@zojw thank you for that mention with an example how to achieve the usually desired behavior.

dpc avatar Aug 06 '22 00:08 dpc

It might be reasonable to use the supports-color crate for this. However, there is a trade-off that would have to be made

  • Use supports-color 2.1.0: Heavyweight dependency, because it uses is-terminal, which has dependencies on libc, windows-sys, ...
  • Use supports-color 3.0.0: Very lightweight dependency, but has a MSRV of 1.70.0

stefnotch avatar Mar 30 '24 15:03 stefnotch