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

Windows color #153

Closed
ghost opened this issue May 16, 2019 · 6 comments · Fixed by #390
Closed

Windows color #153

ghost opened this issue May 16, 2019 · 6 comments · Fixed by #390

Comments

@ghost
Copy link

ghost commented May 16, 2019

Using this file:

package main
import (
   "github.com/rs/zerolog"
   "github.com/rs/zerolog/log"
   "os"
)
func main() {
   log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
   log.Info().Msg("aaaaa bbbbb")
}

I get this result:

[90m7:49PM [0m  [32mINF [0m aaaaa bbbbb

Note that other projects work as expected:

package main
import "github.com/labstack/gommon/log"
func main() {
   log.EnableColor()
   log.Info("aaaaa bbbbb")
}
@rs
Copy link
Owner

rs commented May 23, 2019

I have no Windows experience. If you can help fixing this old issue, you are welcome.

@brightpuddle
Copy link

Windows uses different escape sequences than every other OS. I use this:

https://github.com/mattn/go-colorable

Go-colorable shims os.Stdout and fixes the escape sequences on Windows. Their example is for logrus, but it works for any library that prints colored output. I use it with zerolog ConsoleWriter like this:

zerolog.ConsoleWriter{Out: colorable.NewColorableStdout()}

Just my $.02, but given that we have go-colorable, I don't think this should be "fixed" in every library that prints colored output. Maybe we can link to go-colorable in the zerolog docs and mark this as closed with the solution being to use go-colorable if you need colored Windows output. It's a Windows-specific problem, and Microsoft has started moving in the direction of conforming to the standard. As of recent patches, I think ANSI sequences are available. I'm not a Windows user, but it looks like they're now supported but just not the default:

https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences

There was a good MS blog announcement on this, but I can't seem to find it right now.

@ghost
Copy link
Author

ghost commented Jun 23, 2019

@brightpuddle Windows 7 still has 38% market share by some measurements:

https://netmarketshare.com/operating-system-market-share.aspx?id=platformsDesktopVersions

thats more than Windows 8, all Mac and Linux combined. If you want to hand
wave that away thats your business, but youre only kidding yourself, not me. On
constructive note, this does work:

package main
import (
   "github.com/mattn/go-colorable"
   "github.com/rs/zerolog"
   "github.com/rs/zerolog/log"
)
func main() {
   aa := colorable.NewColorableStdout()
   log.Logger = log.Output(zerolog.ConsoleWriter{Out: aa})
   log.Info().Msg("bb")
}

@rs
Copy link
Owner

rs commented Jun 23, 2019

Nobody is saying it's not worth fixing. I don't know numbers for Go dev on windows compared to other platforms, but it is certainly not aligned with the number of windows users in general. Certainly still big enough to care though.

I'm in favor of adding this dep behind a build flag for windows so the console writer works out of the box. We could also just copy the color codes we need and save the dep and keep the whole thing KISS.

@brightpuddle
Copy link

@cup
rs owns the project--I'm just a user of the project with no say in the direction. Just sharing my own experience.

@gaby
Copy link

gaby commented Jul 8, 2024

@rs This dependency can probably be removed. Anything after Windows 10 supports 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 a pull request may close this issue.

3 participants