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

Detect windows and use non-colored stdout logs? #13

Closed
karalabe opened this issue Jul 2, 2014 · 5 comments
Closed

Detect windows and use non-colored stdout logs? #13

karalabe opened this issue Jul 2, 2014 · 5 comments

Comments

@karalabe
Copy link
Contributor

karalabe commented Jul 2, 2014

I've just ran some code based on log15 under Windows and saw that the output is quite chaotic due to windows not recognizing the color defs and hence dumping a lot of extra characters all over the logs. At least this is my hunch, didn't really have time to track it down. Maybe you could add a build flag to disable colors if running on windows?

@inconshreveable
Copy link
Owner

Ouch, yeah, that's quite possible. What type of terminal were you using, cmd.exe or Powershell, or one of the other variants? For now, I'd suggest you do something like:

if runtime.GOOS == "windows" {
    log.Root().SetHandler(log.StreamHandler(os.Stdout, log.LogfmtFormat()))
}

@karalabe
Copy link
Contributor Author

karalabe commented Jul 8, 2014

I usually use Linux, was just investigating a crash report in windows, so I've got nothing fancy installed, just cmd.exe.

I was also thinking about some similar solution, but since I'm using this in a library (https://github.com/project-iris/iris-go#logging) where anybody can override the default log settings, is there a way to "force" colorless logging even if the user changes some of the settings? (I'm not familiar with the log15 internals, hence my question :) ).

@inconshreveable
Copy link
Owner

You can force colorless logging with this code:

log.Root().SetHandler(log.StreamHandler(os.Stdout, log.LogfmtFormat()))

By default, when log15 init()'s, it checks if os.Stdout is a terminal, and if it is, uses log.TerminalFormat() which does the color formatting. Explicitly setting it to LogfmtFormat() or JsonFormat() will force no colorization.

@ChrisHines
Copy link
Collaborator

What about using the recently announced github.com/mattn/go-colorable package to get color for Windows too?

@inconshreveable
Copy link
Owner

thanks for pointing that out! i'm going to take a look at using mattn's package for this

ChrisHines added a commit that referenced this issue Nov 6, 2014
proper colors on windows thanks to mattn. fixes #13
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

No branches or pull requests

3 participants