logrus
logrus copied to clipboard
Do not assume no color on windows.
recent windows 10 can support colour (it is even enabled in logrus) however the default formatter does not use colour and so the default log format is different on windows to other OSes when logging to the terminal. This removed the hard code no colour for windows terminals to be consistent with other OSes
fixes #763
@jtnord there have been back and forth pull request on this topic. IIRMC, the last pull request desactived coloring on windows, because color support in the terminal depending on the windows version can not been guaranteed. That said, I'm not a windows expert at all. Before merging that, I would like you check the last closded pull request on this topic in order to assess what has been done before.
@jtnord what about this one #864 ? @ceriath do you mind to have a look here ?
Windows 10 has according to this site more than 50% of the windows desktop market and given the updates are pushed pretty hard, and the version that has ANSI support is pretty old now it would suggest that the switch over point to flip the behaviour is here or very close. (see also this page for an old breakdown of versions
I guess whilst we can endlessly debate the use of color vs no color the issue we have is that this drives not the use of colour but the output format, and this change does not address that, it was just a simple enough fix for my needs (#763)
@dgsb the reason for my initial PR on this topic was - as i stated there - not that windows 10 was uncapable of supporting colours, it just was not supported by default, you had to turn it on manually (as in the thread linked by @cup). While definitely is possible to use colours, if you know what you are doing, it will not work for most users that have not explicitly enabled it. People that know, that they enabled it can still use forceColor to have colours, but i think logrus should by default match with the default windows behaviour.
Another fix could be to just check the registry for the key, but i don't think that would be worth the effort and costs
After looking at this more closely I am actually a fan of this. Windows 10 ANSI colors are disabled by default:
https://superuser.com/questions/413073/-/1300251
but they still work if the application specifies it, which Logrus does:
https://github.com/sirupsen/logrus/blob/f0375eb5b588893ff556c71dee32d98e57a9b777/terminal_windows.go#L16
For earlier Windows versions it would help if #965 was fixed as well. Currently with Windows 8.1 this is needed:
package main
import "github.com/mattn/go-colorable"
import "github.com/sirupsen/logrus"
func main() {
logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
logrus.SetOutput(colorable.NewColorableStdout())
logrus.Info("aaaaa bbbbb")
}
and this for Windows 10:
package main
import "github.com/sirupsen/logrus"
func main() {
logrus.SetFormatter(&logrus.TextFormatter{ForceColors: true})
logrus.Info("aaaaa bbbbb")
}
If this issue was fixed as well as #965, this could be used for Linux, Windows 10 and Windows 8.1:
package main
import "github.com/sirupsen/logrus"
func main() {
logrus.Info("aaaaa bbbbb")
}
I didn't notice this PR already existed, but I just opened #974 which addresses the old Windows version issue by detecting when EnableVirtualTerminalProcessing fails, and falling back to no colors in that case. It should allow colors on new Windows versions without impacting output on old versions.