-
-
Notifications
You must be signed in to change notification settings - Fork 187
feat: support NO_COLOR environment variable #411
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,41 @@ | ||
package logger | ||
|
||
import "testing" | ||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestLog(t *testing.T) { | ||
Log("default", "shows this color") | ||
Log("error", "shows this color") | ||
Log("open", "shows this color") | ||
Log("authorized", "shows this color") | ||
Log("skip", "shows this color") | ||
Log("git", "shows this color") | ||
t.Run("with color", func(t *testing.T) { | ||
t.Logf("NO_COLOR: %s", os.Getenv("NO_COLOR")) | ||
SelectLogger() | ||
// info | ||
Log("default", "should be green") | ||
// verbose | ||
Log("git", "should be white") | ||
Log("skip", "should be white") | ||
// notice | ||
Log("authorized", "should be blue") | ||
// warn | ||
Log("open", "should be yellow") | ||
// error | ||
Log("error", "should be red") | ||
}) | ||
|
||
t.Run("without color", func(t *testing.T) { | ||
t.Setenv("NO_COLOR", "true") | ||
t.Logf("NO_COLOR: %s", os.Getenv("NO_COLOR")) | ||
SelectLogger() | ||
// info | ||
Log("default", "should be none") | ||
// verbose | ||
Log("git", "should be none") | ||
Log("skip", "should be none") | ||
// notice | ||
Log("authorized", "should be none") | ||
// warn | ||
Log("open", "should be none") | ||
// error | ||
Log("error", "should be none") | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 should have expected colorine to support NO_COLOR, not eaxh user of the lib to implement that.
I mean it's what fatih/color does
https://github.com/fatih/color/blob/62b78f82738d9b72cd6a159cb33c7be523c0899b/color.go#L40
Did you consider opening the PR on colorine?
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.
colorine is abandoned apparently, last commit is years old.
Maybe using another lib for color could be a better move than adding a band on a wooden leg
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'll try to replace colorine with fatih/color .
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 agree that it's a good idea to leave it up to the coloring logger library whether or not to colorize in order to avoid making the changes too large.
However, changing the library at this stage would have too much impact.
So, at this stage, I would like to propose changing
ghq/logger.logger
to an interface liketype interface logger { Log(string, string) }
and replacing it depending on whether "NO_COLOR" is truthy or not.At this time, you will need to implement something like rawLogger that satisfies the logger interface, but that will not be a major implementation.
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.
By the way, it is common to specify things like
NO_COLOR=1
, and I thought that the truthiness of this could be determined simply by checking whether the string is empty. https://no-color.org also says this.However, there is probably some debate about whether values like “false,” “off,” “0,” and spaces should be considered true, but I think it is OK to treat them as true, following no-color.org.
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.
Sure.
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 agree. Another PR would be a good thing. This one can focus on the nocolor env checking only.
Also if you only need color for the logs, and only the logs, there are slog implementation that comes with colors