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

support NO_COLOR env var for disabling ANSI #11398

Closed
wants to merge 1 commit into from

Conversation

johnbchron
Copy link
Contributor

Objective

Allows disabling ANSI codes in log outputs according to https://no-color.org/. Fixes #11351

Solution

Instead of using the default tracing subscriber formatting layer, we check if the the env variable NO_COLOR equals "1", and if so, set .with_ansi(false).


Changelog

Allows setting the NO_COLOR environment variable to disable ANSI codes in log output.

Migration Guide

N/A (non-breaking, new feature)

@dmlary
Copy link
Contributor

dmlary commented Jan 18, 2024

Should this be needed with the upstream NO_COLOR support in tracing? tokio-rs/tracing#2647

At the lowest level in the tracing crate, the default subscriber checks for this environment variable. Is there something between bevy and the subscriber calling with_ansi(true)?

@@ -173,7 +173,11 @@ impl Plugin for LogPlugin {
#[cfg(feature = "tracing-tracy")]
let tracy_layer = tracing_tracy::TracyLayer::new();

let fmt_layer = tracing_subscriber::fmt::Layer::default().with_writer(std::io::stderr);
let use_color = std::env::var("NO_COLOR").map(|v| v != "1").unwrap_or(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the env variable is present, color should not be used regardless of its value.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially thought the same thing. But if you carefully read the nocolor website, command line flags are supposed to override the environment variable. Because of this, the upstream change allows with_ansi() to override the environment variable, otherwise it wouldn’t be possible for command-line flags to override the environment variable.

Longer explanation in this comment in the upstream issue for the PR tokio-rs/tracing#2388 (comment)

Copy link
Contributor

@dmlary dmlary Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated: Ignore this comment; I found the reference. Leaving this incorrect info for posterity.

Bah, they literally just updated the nocolor website a week ago, and the ~~text about flag overriding has been removed. ~~

If you want to push for NO_COLOR to always override commandline flags, I’d open a new PR upstream to fix the behavior there. The reason can be that the NO_COLOR guidelines changed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it in the code block on the no-color website. It’s the comment at the bottom of their example code that says flags can override environment.

int
main(int argc, char *argv[])
{
	char *no_color = getenv("NO_COLOR");
	bool color = true;

	if (no_color != NULL && no_color[0] != '\0')
		color = false;

	/* do getopt(3) and/or config-file parsing to possibly turn color back on */
	...
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from my understanding the check should be on "not empty". if it's set but empty, keep the colours, if it's set and not empty, then remove the colours

@BD103
Copy link
Member

BD103 commented Jan 18, 2024

Should this be needed with the upstream NO_COLOR support in tracing? tokio-rs/tracing#2647

At the lowest level in the tracing crate, the default subscriber checks for this environment variable. Is there something between bevy and the subscriber calling with_ansi(true)?

This was added to tracing-subscriber 0.3.18. Perhaps this needs a version bump?

@mockersf
Copy link
Member

This was added to tracing-subscriber 0.3.18.

I just checked and this already works with Bevy

@mockersf mockersf closed this Jan 18, 2024
@johnbchron
Copy link
Contributor Author

Yeah I didn't even think to check whether this was already implemented with tracing. Thanks!

@johnbchron johnbchron deleted the support-no-color branch January 18, 2024 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feature request: Enable/disable ANSI or colored output logging
5 participants