Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only colorize output when outputting to a TTY #1160

Open
olix0r opened this issue Dec 29, 2020 · 6 comments
Open

Only colorize output when outputting to a TTY #1160

olix0r opened this issue Dec 29, 2020 · 6 comments
Labels
crate/subscriber Related to the `tracing-subscriber` crate kind/feature New feature or request

Comments

@olix0r
Copy link
Member

olix0r commented Dec 29, 2020

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.

@hawkw hawkw added the kind/feature New feature or request label Dec 30, 2020
@jyn514
Copy link
Contributor

jyn514 commented Jan 3, 2021

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.

@hawkw
Copy link
Member

hawkw commented Jan 4, 2021

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 hawkw added the crate/subscriber Related to the `tracing-subscriber` crate label Jan 4, 2021
@coderedart
Copy link

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

@hawkw
Copy link
Member

hawkw commented Jan 24, 2022

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..

@dpc
Copy link
Contributor

dpc commented Aug 6, 2022

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

@stefnotch
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/subscriber Related to the `tracing-subscriber` crate kind/feature New feature or request
Projects
None yet
Development

No branches or pull requests

6 participants