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

feat: support NO_COLOR environment variable #411

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

hezhizhen
Copy link
Contributor

Implement #330

Comment on lines +58 to +62
v := os.Getenv("NO_COLOR")

if strings.ToLower(v) == "true" {
logger = loggerWithoutColor
}

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?

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

Copy link
Contributor Author

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 .

Copy link
Member

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 like type 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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure.

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 like type 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.

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

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.

3 participants