Logrus Prefixed Log Formatter

Build Status

Logrus formatter mainly based on original logrus.TextFormatter but with slightly modified colored output and support for log entry prefixes, e.g. message source followed by a colon. In addition, custom color themes are supported.

Formatter screenshot

Just like with the original logrus.TextFormatter when a TTY is not attached, the output is compatible with the logfmt format:

text time="Oct 27 00:44:26" level=debug msg="Started observing beach" animal=walrus number=8 time="Oct 27 00:44:26" level=info msg="A group of walrus emerges from the ocean" animal=walrus size=10 time="Oct 27 00:44:26" level=warning msg="The group's number increased tremendously!" number=122 omg=true time="Oct 27 00:44:26" level=debug msg="Temperature changes" temperature=-4 time="Oct 27 00:44:26" level=panic msg="It's over 9000!" animal=orca size=9009 time="Oct 27 00:44:26" level=fatal msg="The ice breaks!" number=100 omg=true exit status 1


To install formatter, use go get:

sh $ go get github.com/x-cray/logrus-prefixed-formatter


Here is how it should be used:

```go package main

import ( "github.com/Sirupsen/logrus" prefixed "github.com/x-cray/logrus-prefixed-formatter" )

var log = logrus.New()

func init() { log.Formatter = new(prefixed.TextFormatter) log.Level = logrus.DebugLevel }

func main() { log.WithFields(logrus.Fields{ "prefix": "main", "animal": "walrus", "number": 8, }).Debug("Started observing beach")

    "prefix":      "sensor",
    "temperature": -4,
}).Info("Temperature changes")

} ```


prefixed.TextFormatter exposes the following fields and methods.



SetColorScheme(colorScheme *prefixed.ColorScheme)

Sets an alternative color scheme for colored output. prefixed.ColorScheme struct supports the following fields: * InfoLevelStyle string — info level style. * WarnLevelStyle string — warn level style. * ErrorLevelStyle string — error style. * FatalLevelStyle string — fatal level style. * PanicLevelStyle string — panic level style. * DebugLevelStyle string — debug level style. * PrefixStyle string — prefix style. * TimestampStyle string — timestamp style.

Color styles should be specified using mgutz/ansi style syntax. For example, here is the default theme:

go InfoLevelStyle: "green", WarnLevelStyle: "yellow", ErrorLevelStyle: "red", FatalLevelStyle: "red", PanicLevelStyle: "red", DebugLevelStyle: "blue", PrefixStyle: "cyan", TimestampStyle: "black+h"

It's not necessary to specify all colors when changing color scheme if you want to change just specific ones:

go formatter.SetColorScheme(&prefixed.ColorScheme{ PrefixStyle: "blue+b", TimestampStyle: "white+h", })