Logrus: Enable ANSI colors for Windows 10 and newer

Created on 27 Feb 2017  路  13Comments  路  Source: sirupsen/logrus

Hey!

I've been reading a bit about Windows CMD and Virtual Processing. This code bellow is enough to make ANSI work on Windows 10 and newer:

handle := syscall.Handle(os.Stdout.Fd())
kernel32DLL := syscall.NewLazyDLL("kernel32.dll")
setConsoleModeProc := kernel32DLL.NewProc("SetConsoleMode")
setConsoleModeProc.Call(uintptr(handle), 0x0001|0x0002|0x0004)

More information about it here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686033(v=vs.85).aspx

It would be interesting to add!

Most helpful comment

It looks like this issue has been resolved. Closing.
Please let me know if this isn't the case.

All 13 comments

Indeed it would be nice to be able to see coloured output rather than the following in Windows 10:

?[34mINFO?[0m[0000] A walrus appears                              ?[34manimal?[0m=walrus

The addition of that code would solve that. What do you think @sirupsen?

It does solve the issue, I've tested it myself. Tricky things will be making sure this runs only in Windows.

Maybe creating a PR would move this faster (thought looking at the PR list, maybe no so much :D)

Done @ezk84 馃槃 Let's see what happens.

@hacdias I've added a PR for your PR, where I make a few alterations to make it more likely to be accepted into @sirupsen's repo.

Done! Thank you 馃槃

It looks like this issue has been resolved. Closing.
Please let me know if this isn't the case.

Continues to be an issue for me, this is what my console looks like:

?[36mINFO?[0m[0000] Loaded module: Help
?[37mDEBU?[0m[0002] hi
?[37mDEBU?[0m[0003] Error sending message.                        ?[37merror?[0m="HTTP 400 Bad Request, {\"embed\": [\"url\"]}"

Using Ubuntu on Windows (in cmd) it seems to work, just not in a standard cmd window.

Make sure this isn't checked:

image

It isn't, still doesn't work.
cmd Properties

I just checked the code and they removed what I have added. Check this: https://github.com/sirupsen/logrus#formatters

Indeed, commit e66f229 seems to be where it was removed.
Following README's instruction:

For Windows, see github.com/mattn/go-colorable.

go get github.com/mattn/go-colorable

Then setting logrus.SetOutput(colorable.NewColorableStdout()) fixed this for me, in case others were wondering.

yes, this issue +1,
seems #471 does not work

Was this page helpful?
0 / 5 - 0 ratings