Logrus: Logging on Windows

Created on 3 May 2015  路  13Comments  路  Source: sirupsen/logrus

On Windows logrus outputs terminal control sequences: https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/issues/6

How to make it readable?

Most helpful comment

Just for reference to anyone who wants color output in windows and stumbled upon this thread; Here's how to do it:

// import this library
import "github.com/shiena/ansicolor"

// force colors on for TextFormatter
log.Formatter = &logrus.TextFormatter{
    ForceColors: true,
}

// then wrap the log output with it
log.Out = ansicolor.NewAnsiColorWriter(os.Stdout)

All 13 comments

Yeah, the color coding wouldn't work on Windows. I don't have a Windows machine to test a fix on, mind sending a fix?

@Sirupsen Wouldn't it be better to enable coloring only when getenv("TERM") starts with xterm?

Windows is very well able to do coloring as well, for one there's the "color" command, other then that I only know about the C way of doing it, I didn't dive much deeper into it then that, but I'm pretty sure you can do those multi colored lines as well.

Apparently this is able to do it quite well (used a quick search): https://github.com/adoxa/ansicon

@neico This is alternative terminal that does support ANSI. It will not work with escape codes as it works now.

https://support.microsoft.com/en-us/kb/101875
http://blog.mmediasys.com/2010/11/24/we-all-love-colors/

@Sirupsen would you accept PR which would output colors only if TERM is set to anything starting with xterm? Otherwise it would output messages without colors?

@Sirupsen I tested that on Windows and no color codes output.

@freeformz yeah I wouldn't expect colours on Windows with that change, just that it wouldn't print the colour codes not recognized on win.

@Sirupsen Yes. I was confirming that this works as expected on windows. There were no colors or color codes output.

Thanks :)

Just for reference to anyone who wants color output in windows and stumbled upon this thread; Here's how to do it:

// import this library
import "github.com/shiena/ansicolor"

// force colors on for TextFormatter
log.Formatter = &logrus.TextFormatter{
    ForceColors: true,
}

// then wrap the log output with it
log.Out = ansicolor.NewAnsiColorWriter(os.Stdout)

I know this is long closed, but an alternative solution exists on Windows 10

func main() {
    var originalMode uint32
    stdout := windows.Handle(os.Stdout.Fd())

    windows.GetConsoleMode(stdout, &originalMode)
    windows.SetConsoleMode(stdout, originalMode | windows.ENABLE_VIRTUAL_TERMINAL_PROCESSING)
    defer windows.SetConsoleMode(stdout, originalMode)

    log.Info("In glorious colour")
}

log.Formatter= and log.Out= throw errors for me, instead I use:

import (
  "github.com/shiena/ansicolor"
  log "github.com/sirupsen/logrus"
  "os"
)

func main(){
  log.SetFormatter(&log.TextFormatter{ForceColors: true})
  log.SetOutput(ansicolor.NewAnsiColorWriter(os.Stdout))
  log.Info("hello")
}
Was this page helpful?
0 / 5 - 0 ratings