-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
feat(common): do not use colors in CLI if not supported #9278
feat(common): do not use colors in CLI if not supported #9278
Conversation
Pull Request Test Coverage Report for Build 7bb12429-bf59-48c9-94c0-233fad22f796
💛 - Coveralls |
type ColorTextFn = (text: string) => string; | ||
|
||
const isColorAllowed = () => !process.env.NO_COLOR; | ||
const isColorAllowed = () => !process.env.NO_COLOR && WriteStream.prototype.hasColors(); |
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 is mostly only used by the ConsoleLogger
, but there are references to it in the Injector.ts
file as well. If it was just the ConsoleLogger
I'd say this is okay, or even checking process.stdout.hasColors()
directly instead of needing the prototype, but I'm not sure how to handle if this check is coming from the Injector.ts
Overall, I don't think this should be an issue, my only concern would be if someone is in an environment where hasColors()
returns false
but they want to force the use of 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.
even checking
process.stdout.hasColors()
directly instead of needing the prototype
That only checks stdout
and not stderr
. See nodejs/node#40236 and nodejs/node#40240 for discussion on exposing a better way to access Writestream.prototype.hasColors()
.
but I'm not sure how to handle if this check is coming from the
Injector.ts
isColorAllowed()
could be refactored to allow an optional parameter of the stream that is being written to and call .hasColors()
on that directly.
Overall, I don't think this should be an issue, my only concern would be if someone is in an environment where
hasColors()
returnsfalse
but they want to force the use of colors.
They can use the FORCE_COLOR
environment variable.
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.
cool!
can you run npm run format
(or just copy & paste)
would be
import { WriteStream } from 'tty';
const isColorAllowed = () =>
!process.env.NO_COLOR && WriteStream.prototype.hasColors();
LGTM |
Tips: For those who use Docker, do not forget to use the option tty in docker-compose service, otherwise the colors will not be displayed from this feature.
|
I think previously colors were displayed even if |
Yes before, the colors were displayed without passing tty param I then tried to add this parameter to docker-compose:
But it does not work better. So I use the logger without color now :( What I tried without success:
|
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
isColorAllowed()
will returntrue
even when the terminal can't support colors.Issue Number: N/A
What is the new behavior?
Colors will be enabled if the
NO_COLOR
environment variable is not an empty string andtty.WriteStream.prototype.hasColors()
returnstrue
.Requires Node.js v10.16.0 or newer.
This means that users of the default NestJS logger can avoid having broken ANSI escape codes in their logs without any extra configuration.
Does this PR introduce a breaking change?
Other information
On the off chance someone is logging to a TTY that supports color, but
tty.WriteStream.prototype.hasColors()
says it doesn't, users can use the built-inFORCE_COLOR
environment variable to override what is detected.