-
Notifications
You must be signed in to change notification settings - Fork 721
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
subscriber: add ability to disable ANSI without crate feature #2532
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, I'm not sure if I like having an API with two separate ways to disable ANSI colors (with_ansi(false)
and disable_ansi()
), that seems a bit confusing for users. As an alternative solution, what do you think about changing with_ansi
to no longer require the "ansi" feature flag, and having it print a warning (and possibly a debug_assert!
) if the argument is true
and the "ansi" feature flag is disabled?
I took this idea from
Sounds good to me! |
Was just about to implement this when I realized I'm not sure how to "print" a warning here. Tracing isn't set up at this point yet, so should I be using |
|
Currently it is not possible to disable ANSI in `fmt::Subscriber` without enabling the `ansi` crate feature. This makes it difficult for users to implement interoperable settings that are controllable with crate features without having to pull in the dependencies `ansi` does. This removes the crate feature requirement on `fmt::Subscriber::with_ansi()`, but will print a warning if trying to enable ANSI terminal colors without the crate feature.
#[cfg(not(feature = "ansi"))] | ||
if ansi { | ||
const ERROR: &str = | ||
"tracing-subscriber: enabled ANSI terminal colors without the `ansi` crate feature"; | ||
#[cfg(debug_assertions)] | ||
panic!("{}", ERROR); | ||
#[cfg(not(debug_assertions))] | ||
eprintln!("{}", ERROR); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope this is appropriate.
Should I add some documentation on with_ansi()
about the warning? I didn't feel it's needed really.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should document that enabling ANSI formatting requires the feature flag. Now that the method itself is no longer feature gated, there's no obvious way to determine from the documentation that the feature flag is necessary.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this looks great! I had some suggestions for rewording the documentation and the error message --- let me know what you think?
#[cfg(not(feature = "ansi"))] | ||
if ansi { | ||
const ERROR: &str = | ||
"tracing-subscriber: enabled ANSI terminal colors without the `ansi` crate feature"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This error message describes what happened, rather than why it happened, which might be more helpful for the user. How about rephrasing it as something like this:
"tracing-subscriber: enabled ANSI terminal colors without the `ansi` crate feature"; | |
"tracing-subscriber: the 'ansi' crate feature is required to enable ANSI terminal colors"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's way better indeed.
I found that in other parts of the documentation in tracing
are using `
instead of '
with crate features, so I used it here too. WDYT?
#[cfg(not(feature = "ansi"))] | ||
if ansi { | ||
const ERROR: &str = | ||
"tracing-subscriber: enabled ANSI terminal colors without the `ansi` crate feature"; | ||
#[cfg(debug_assertions)] | ||
panic!("{}", ERROR); | ||
#[cfg(not(debug_assertions))] | ||
eprintln!("{}", ERROR); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should document that enabling ANSI formatting requires the feature flag. Now that the method itself is no longer feature gated, there's no obvious way to determine from the documentation that the feature flag is necessary.
Co-Authored-By: Eliza Weisman <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great to me!
## Motivation Currently it is not possible to disable ANSI in `fmt::Subscriber` without enabling the "ansi" crate feature. This makes it difficult for users to implement interoperable settings that are controllable with crate features without having to pull in the dependencies "ansi" does. I hit this while writing an application with multiple logging options set during compile-time and I wanted to cut down on dependencies if possible. ## Solution This changes `fmt::Subscriber::with_ansi()` to not require the "ansi" feature flag. This way, `with_ansi(false)` can be called even when the "ansi" feature is disabled. Calling `with_ansi(true)` when the "ansi" feature is not enabled will panic in debug mode, or print a warning if debug assertions are disabled. Co-authored-by: Eliza Weisman <[email protected]>
## Motivation Currently it is not possible to disable ANSI in `fmt::Subscriber` without enabling the "ansi" crate feature. This makes it difficult for users to implement interoperable settings that are controllable with crate features without having to pull in the dependencies "ansi" does. I hit this while writing an application with multiple logging options set during compile-time and I wanted to cut down on dependencies if possible. ## Solution This changes `fmt::Subscriber::with_ansi()` to not require the "ansi" feature flag. This way, `with_ansi(false)` can be called even when the "ansi" feature is disabled. Calling `with_ansi(true)` when the "ansi" feature is not enabled will panic in debug mode, or print a warning if debug assertions are disabled. Co-authored-by: Eliza Weisman <[email protected]>
# 0.3.17 (April 21, 2023) This release of `tracing-subscriber` fixes a build error when using `env-filter` with recent versions of the `regex` crate. It also introduces several minor API improvements. ### Fixed - **env-filter**: Add "unicode-case" and "unicode-perl" to the `regex` dependency, fixing a build error with recent versions of `regex` (#2566) - A number of minor documentation typos and other fixes (#2384, #2378, #2368, #2548) ### Added - **filter**: Add `fmt::Display` impl for `filter::Targets` (#2343) - **fmt**: Made `with_ansi(false)` no longer require the "ansi" feature, so that ANSI formatting escapes can be disabled without requiring ANSI-specific dependencies (#2532) ### Changed - **fmt**: Dim targets in the `Compact` formatter, matching the default formatter (#2409) Thanks to @keepsimple1, @andrewhalle, @LeoniePhiline, @LukeMathWalker, @howardjohn, @daxpedda, and @dbidwell94 for contributing to this release!
This release of `tracing-subscriber` fixes a build error when using `env-filter` with recent versions of the `regex` crate. It also introduces several minor API improvements. - **env-filter**: Add "unicode-case" and "unicode-perl" to the `regex` dependency, fixing a build error with recent versions of `regex` (tokio-rs#2566) - A number of minor documentation typos and other fixes (tokio-rs#2384, tokio-rs#2378, tokio-rs#2368, tokio-rs#2548) - **filter**: Add `fmt::Display` impl for `filter::Targets` (tokio-rs#2343) - **fmt**: Made `with_ansi(false)` no longer require the "ansi" feature, so that ANSI formatting escapes can be disabled without requiring ANSI-specific dependencies (tokio-rs#2532) - **fmt**: Dim targets in the `Compact` formatter, matching the default formatter (tokio-rs#2409) Thanks to @keepsimple1, @andrewhalle, @LeoniePhiline, @LukeMathWalker, @howardjohn, @daxpedda, and @dbidwell94 for contributing to this release!
…rs#2532) ## Motivation Currently it is not possible to disable ANSI in `fmt::Subscriber` without enabling the "ansi" crate feature. This makes it difficult for users to implement interoperable settings that are controllable with crate features without having to pull in the dependencies "ansi" does. I hit this while writing an application with multiple logging options set during compile-time and I wanted to cut down on dependencies if possible. ## Solution This changes `fmt::Subscriber::with_ansi()` to not require the "ansi" feature flag. This way, `with_ansi(false)` can be called even when the "ansi" feature is disabled. Calling `with_ansi(true)` when the "ansi" feature is not enabled will panic in debug mode, or print a warning if debug assertions are disabled. Co-authored-by: Eliza Weisman <[email protected]>
# 0.3.17 (April 21, 2023) This release of `tracing-subscriber` fixes a build error when using `env-filter` with recent versions of the `regex` crate. It also introduces several minor API improvements. ### Fixed - **env-filter**: Add "unicode-case" and "unicode-perl" to the `regex` dependency, fixing a build error with recent versions of `regex` (tokio-rs#2566) - A number of minor documentation typos and other fixes (tokio-rs#2384, tokio-rs#2378, tokio-rs#2368, tokio-rs#2548) ### Added - **filter**: Add `fmt::Display` impl for `filter::Targets` (tokio-rs#2343) - **fmt**: Made `with_ansi(false)` no longer require the "ansi" feature, so that ANSI formatting escapes can be disabled without requiring ANSI-specific dependencies (tokio-rs#2532) ### Changed - **fmt**: Dim targets in the `Compact` formatter, matching the default formatter (tokio-rs#2409) Thanks to @keepsimple1, @andrewhalle, @LeoniePhiline, @LukeMathWalker, @howardjohn, @daxpedda, and @dbidwell94 for contributing to this release!
Motivation
Currently it is not possible to disable ANSI in
fmt::Subscriber
without enabling theansi
crate feature. This makes it difficult for users to implement interoperable settings that are controllable with crate features without having to pull in the dependenciesansi
does.I hit this while writing an application with multiple logging options set during compile-time and I wanted to cut down on dependencies if possible.
Solution
This adds a simple
fmt::Subscriber::disable_ansi()
that is available even without theansi
crate feature.