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

Disable ANSI for tracing-subscriber output? #20

Closed
jstarks opened this issue Oct 28, 2021 · 7 comments
Closed

Disable ANSI for tracing-subscriber output? #20

jstarks opened this issue Oct 28, 2021 · 7 comments

Comments

@jstarks
Copy link

jstarks commented Oct 28, 2021

The default behavior of tracing-subscriber is to include color control codes in output. It does not try to detect whether the output is a terminal as part of this: tokio-rs/tracing#1160.

For test output this is pretty bad, since it means the color control codes get into logs and end up causing downstream log processing code to complain. Would it be reasonable to disable ANSI output (via SubscriberBuilder::with_ansi) in expand_tracing_init()? I'd be happy to send a patch.

@d-e-s-o
Copy link
Owner

d-e-s-o commented Oct 29, 2021

I think it would be fine, as long as the check does not introduce another dependency. So pulling in libc for isatty is probably a no-go. But checking RUST_LOG_COLOR should be okay if that's what you had in mind.
Also, it seems as if env_logger honors RUST_LOG_STYLE already and we should make sure to create a proper facade for both supported crates.
Feel free to create a patch. I don't have many cycles to merge it right now, but it should happen in the next few weeks or so.

@jstarks
Copy link
Author

jstarks commented Oct 29, 2021

Ah, OK, I was originally thinking we could just always disable ANSI for test output, but maybe it's useful for interactive cargo test invocations... In that case, probably keying off of RUST_LOG_STYLE would be reasonable so that it just works for both cases.

@ostollmann
Copy link

Maybe this could also support NO_COLOR.

kingli-crypto added a commit to kingli-crypto/test-log that referenced this issue Oct 10, 2023
kingli-crypto added a commit to kingli-crypto/test-log that referenced this issue Oct 10, 2023
kingli-crypto added a commit to kingli-crypto/test-log that referenced this issue Oct 10, 2023
kingli-crypto added a commit to kingli-crypto/test-log that referenced this issue Oct 10, 2023
@kingli-crypto
Copy link

I tied to implement this, seems SubscriberBuilder::with_ansi require the ansi trait.

Do you think should it be enabled by default? Or guarded by another new trait so we don't break backward compatibility

@d-e-s-o
Copy link
Owner

d-e-s-o commented Oct 12, 2023

I tied to implement this, seems SubscriberBuilder::with_ansi require the ansi trait.

Nice!

Do you think should it be enabled by default?

By default we should keep colors, in my opinion. It's how everything else behaves by default.

I'd say if we follow the NO_COLOR "standard" then we should do it according to their playbook. Right now if I set NO_COLOR=1 I do get colors, which is very confusing conceptually and not in line with their suggested behavior. As per their documentation:

	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 */

So according to that we should just check for the existence of the env variable. Though I suppose special casing false and 0 to re-enable coloring (and disable in all other cases) would be fine, but I don't feel strongly.

@d-e-s-o
Copy link
Owner

d-e-s-o commented Oct 12, 2023

Also (and I haven't tested that), as mentioned earlier currently it seems as if env_logger honors RUST_LOG_STYLE. So I think it would be better to do the same instead of introducing our own variable name.

@d-e-s-o
Copy link
Owner

d-e-s-o commented Apr 29, 2024

NO_COLOR should now be honored properly (#50).

@d-e-s-o d-e-s-o closed this as completed Apr 29, 2024
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

No branches or pull requests

4 participants