log15 is a powerful structured logging package for Go with a simple, easy-to-use API. It supports output with color when logging to terminal, and also can write to files, streams, syslog and network.

log15 is a good alternative to the standard Go log package.

Installing and using log15

log15 is available as a versioned package via gopkg.in. You can install it with

go get gopkg.in/inconshreveable/log15.v2

Then import the package, creating a convenient alias for it (we will use log):

import log "gopkg.in/inconshreveable/log15.v2"

Logging

There are five default logging levels in log15: Debug, Info, Warn, Error and Crit. Each function accepts a message string as the first argument and any number of key-value pairs that follow.

For example, this code:

package main

import log "gopkg.in/inconshreveable/log15.v2"

func main() {
        log.Warn("this is a message", "answer", 42, "question", nil)
        log.Error("there was an error", "oops", "sorry")
        log.Crit("boom")
}

will output:

WARN[01-13|22:02:53] this is a message        answer=42 question=nil
EROR[01-13|22:29:23] there was an error       oops=sorry
CRIT[01-13|22:29:40] boom 

Using logger contexts

Loggers can have hierarchy. You setup a context and then call logging methods on it. Actually, the mentioned above Crit, Error, etc. are just methods on the root context. Logging context sets up default key-values for its methods. For example, you can have a context per network request, recording remote address:

reqlog := log.New("addr", request.RemoteAddr())
reqlog.Info("received request")
reqlog.Error("error opening file", "name", filename)
INFO[01-13|22:41:00] received request     addr=127.0.0.1
EROR[01-13|22:41:00] error opening file   addr=127.0.0.1 name=config.json

Notice that we didn't pass addr to Info and Error, but it got outputted anyway because we specified it when we created a context.

We can derive more contexts from the current one by simply calling the New method (e.g reqlog.New() for our example).

Specifying logger output

log15 has a flexible output handler system. Output handlers can be attached to contexts:

reqlog.SetHandler(log.MultiHandler(
    log.StreamHandler(os.Stderr, log.LogfmtFormat()),
    log.LvlFilterHandler(
        log.LvlError,
        log.Must.FileHandler("errors.json", log.JsonHandler())))

Refer to handlers documentation for more information.

Handlers can have formatters. For example, here's how to get JSON output with our modified example:

package main

import (
        "os"
        log "gopkg.in/inconshreveable/log15.v2"
)

func main() {
        l := log.New()
        l.SetHandler(log.StreamHandler(os.Stderr, log.JsonFormat()))
        l.Crit("planet destroyed", "answer", 42)
}

Output:

{"answer":42,"lvl":0,"msg":"planet destroyed","t":"2015-01-13T23:03:13.341434194+01:00"}

Where to get log15

GitHub: https://github.com/inconshreveable/log15 Documentation: http://godoc.org/github.com/inconshreveable/log15 Author: Alan Shreve License: Apache License 2.0