Structured, pluggable logging for Go.
Go to file
Burke Libbey ce9bacf7b3 quickhax 2014-03-04 11:38:21 -05:00
README.md README: rewrite 2014-02-23 20:19:34 -05:00
entry.go quickhax 2014-03-04 11:38:21 -05:00
examples_test.go Fix examples formatting 2014-02-24 06:35:56 -05:00
logger.go Code review fixes 2014-02-24 06:34:12 -05:00
logrus.go split into multiple files 2014-02-23 19:50:42 -05:00

README.md

Logrus

Logrus is a simple, opinionated logging package for Go. Features include:

  • Level logging. Logrus has the levels: Debug, Info, Warning and Fatal.
  • Exceptions. Warnings will log as an exception along with logging it to out, without quitting. Fatal will do the same, but call os.Exit(1) after emitting the exception.
  • JSON. Logrus currently logs as JSON by default.

The API is completely compatible with the Go standard lib logger, with only the features above added.

Motivation

The motivation for this library came out of a pattern seen in Go applications me and others have been writing with functions such as:

func reportFatalError(err error) {
  airbrake.Notify(err)
  log.Fatal(err)
}

func reportWarning(err error) {
  airbrake.Notify(err)
}

JSON logging is excellent for parsing logs for analysis and troubleshooting. It's supported natively by log aggregators such as logstash and Splunk. Logging JSON with logrus with the WithFields and WithField API in logrus forces you to think about what context to log, to provide valuable troubleshoot information later.

Example

import (
  "github.com/Sirupsen/logrus"
)

var logger logrus.New()
func main() {
  logger.WithFields(Fields{
      "animal":   "walrus",
      "location": "New York Aquarium",
      "weather":  "rain",
      "name":     "Wally",
      "event":    "escape",
      }).Info("Walrus has escaped the aquarium! Action required!")
  // {
  //   "level": "info",
  //   "animal": "walrus",
  //   "location": "New York Aquarium",
  //   "weather":"rain",
  //   "name": "Wally",
  //   "event":"escape",
  //   "msg": "Walrus has escaped the aquarium! Action required!")
  //   "time": "2014-02-23 19:57:35.862271048 -0500 EST"
  // }

  logger.WithField("source", "kafka").Infof("Connection to Kafka failed with %s", "some error")
  // {
  //   "level": "info",
  //   "source": "kafka",
  //   "msg": "Connection to Kafka failed with some error",
  //   "time": "2014-02-23 19:57:35.862271048 -0500 EST"
  // }
}

Using Warning and Fatal to log to airbrake requires setting airbrake.Endpoint and airbrake.ApiKey. See tobi/airbrake-go.