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

fmt subscriber should honor NO_COLOR by default #2388

Closed
matklad opened this issue Nov 16, 2022 · 6 comments · Fixed by #2647
Closed

fmt subscriber should honor NO_COLOR by default #2388

matklad opened this issue Nov 16, 2022 · 6 comments · Fixed by #2647
Labels
crate/subscriber Related to the `tracing-subscriber` crate good first issue Good for newcomers kind/feature New feature or request

Comments

@matklad
Copy link
Contributor

matklad commented Nov 16, 2022

https://no-color.org

Would help as a sure-way to neuter colors if you really don't want them for whatever reason.

@davidbarsky
Copy link
Member

I'm not opposed to this change, especially since there was interest in the past to have color controlled by an env variable (#1160 (comment)). Naively, having fmt::Layer read an environment variable (e.g., NO_COLOR, but it should be overridable through a builder method) and setting fmt::Layer::with_ansi to false is reasonable, but I'm not sure whether reading this variable should be on by default or what the behavior should be if/when the user has affirmatively set with_ansi(true). What are your expectations for this?

@hawkw
Copy link
Member

hawkw commented Nov 16, 2022

but I'm not sure whether reading this variable should be on by default or what the behavior should be if/when the user has affirmatively set with_ansi(true). What are your expectations for this?

I think the variable is intended to disable colors if it is set, but it not being set should not be considered affirmatively enabling colors. We should probably read it by default at least in all the cases where we would default to enabling ANSI colors, and disable ANSI colors if it is set.

I'm less sure what the correct behavior should be in the case where the user explicitly calls with_ansi. Open to suggestions!

@hawkw hawkw added kind/feature New feature or request good first issue Good for newcomers crate/subscriber Related to the `tracing-subscriber` crate labels Nov 16, 2022
@matklad
Copy link
Contributor Author

matklad commented Nov 18, 2022

I'm less sure what the correct behavior should be in the case where the user explicitly calls with_ansi. Open to suggestions!

My reading of no-color.org is that NO_COLOR is only intended to suppress default colors, so with_ansi should not be affected.

I guess, in the ideal world we'd do

fn with_ansi(Ansi::Yes | Ansi::No | Ansi::Auto /* based on is_term and NO_COLOR */)

but with today's API, I think the best action is not change the API in any way, and just check for NO_COLOR if with_ansi was not explicitly set.

@andrewpollack
Copy link
Contributor

Looking into this issue, and I wanted to confirm. My interpretation is that setting ’NO_COLOR’ means that color is turned off (even in the case of ’with_ansi’).

Is this the desired behavior? Do other libraries/software have precedence of overwriting the ’NO_COLOR’ case?

@dmlary
Copy link
Contributor

dmlary commented Jul 12, 2023

I found this issue after discovering NO_COLOR didn't work for bevy trace output

From https://no-color.org/:

Command-line software which adds ANSI color to its output by default should check for a NO_COLOR environment variable that, when present and not an empty string (regardless of its value), prevents the addition of ANSI color.

This reads as "if NO_COLOR is set to any non-empty value, the program should never output ANSI colors". This reading is based on the NO_COLOR being an accessibility flag, not a per-utility configuration.

This is counter to some of the previous comments allowing the setting to be overridden. Allowing it to be overridden a) defeats the purpose of the flag, or b) requires every library author to duplicate the code for checking NO_COLOR.

I'm happy to open a PR for this.

dmlary added a commit to dmlary/tracing that referenced this issue Jul 13, 2023
The `NO_COLOR` environment variable should disable all ANSI color
output (https:://no-color.org).

This commit updates `fmt::Subscriber` to check the `NO_COLOR`
environment variable before enabling ANSI color output.  As described in
the spec, any non-empty value set for `NO_COLOR` will prevent `is_ansi`
from being set to `true`.

fixes tokio-rs#2388
@dmlary
Copy link
Contributor

dmlary commented Jul 13, 2023

Baaaah, I re-read the no-color website.

I found this issue after discovering NO_COLOR didn't work for bevy trace output

From https://no-color.org/:

Command-line software which adds ANSI color to its output by default should check for a NO_COLOR environment variable that, when present and not an empty string (regardless of its value), prevents the addition of ANSI color.

This reads as "if NO_COLOR is set to any non-empty value, the program should never output ANSI colors". This reading is based on the NO_COLOR being an accessibility flag, not a per-utility configuration.

Bahhhh, I re-read the website and realized later they explicitly allow overriding the NO_COLOR environment variable with a command-line flag.

Allowing it to be overridden a) defeats the purpose of the flag, or b) requires every library author to duplicate the code for checking NO_COLOR.

I'm still thinking along these lines though. We don't want the users of tracing to need to explicitly check NO_COLOR themselves. I'll update the PR to only set the default value of is_ansi based on NO_COLOR. If the caller explicitly sets ANSI with with_ansi or set_ansi, that will override the environment variable.

hawkw pushed a commit that referenced this issue Aug 17, 2023
## Motivation

It's necessary at times to be able to disable ANSI color output for 
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in #2388

## Solution

This commit updates `fmt::Subscriber` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes #2388
davidbarsky pushed a commit that referenced this issue Sep 26, 2023
It's necessary at times to be able to disable ANSI color output for
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in #2388

This commit updates `fmt::Subscriber` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes #2388
davidbarsky pushed a commit that referenced this issue Sep 27, 2023
It's necessary at times to be able to disable ANSI color output for
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in #2388

This commit updates `fmt::Subscriber` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes #2388
davidbarsky pushed a commit that referenced this issue Sep 27, 2023
It's necessary at times to be able to disable ANSI color output for
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in #2388

This commit updates `fmt::Subscriber` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes #2388
davidbarsky pushed a commit that referenced this issue Sep 27, 2023
It's necessary at times to be able to disable ANSI color output for
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in #2388

This commit updates `fmt::Layer` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes #2388
davidbarsky pushed a commit that referenced this issue Sep 27, 2023
It's necessary at times to be able to disable ANSI color output for
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in #2388

This commit updates `fmt::Layer` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes #2388
davidbarsky pushed a commit that referenced this issue Sep 29, 2023
It's necessary at times to be able to disable ANSI color output for
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in #2388

This commit updates `fmt::Layer` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes #2388
hawkw pushed a commit that referenced this issue Oct 1, 2023
It's necessary at times to be able to disable ANSI color output for
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in #2388

This commit updates `fmt::Layer` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes #2388
aaronriekenberg added a commit to aaronriekenberg/rust-parallel that referenced this issue Mar 3, 2024
kaffarell pushed a commit to kaffarell/tracing that referenced this issue May 22, 2024
It's necessary at times to be able to disable ANSI color output for
rust utilities using `tracing`.  The informal standard for this is the
`NO_COLOR` environment variable described here: https://no-color.org/

Further details/discussion in tokio-rs#2388

This commit updates `fmt::Layer` to check the `NO_COLOR`
environment variable when determining whether ANSI color output is
enabled by default. As described in the spec, any non-empty value set
for `NO_COLOR` will cause ANSI color support to be disabled by default.

If the user manually overrides ANSI color support, such as by calling
`with_ansi(true)`, this will still enable ANSI colors, even if
`NO_COLOR` is set. The `NO_COLOR` env var only effects the default
behavior.

Fixes tokio-rs#2388
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 good first issue Good for newcomers kind/feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants