Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
63 lines (50 sloc) 2.1 KB

CircleCI Go Report Card GoDoc MIT License

dlog (delegating log) wraps common functionality for common golang logging packages.

The dlog.Logger interface wraps the common logging functionality. Every method on dlog.Logger is also a global method on the dlog package. Given an implementation of dlog.Logger, you can register it as the global logger by calling:

func register(logger dlog.Logger) {
  dlog.SetLogger(logger)
}

To make things simple, packages for glog, logrus, log15, and lion are given with the ability to easily register their implementations as the default logger:

import (
  "go.pedge.io/dlog/glog"
  "go.pedge.io/dlog/lion"
  "go.pedge.io/dlog/log15"
  "go.pedge.io/dlog/logrus"
  "go.pedge.io/dlog/zap"
)

func registrationFunctions() {
  dlog_glog.Register() // set glog as the global logger
  dlog_lion.Register() // set lion as the global logger with default settings
  dlog_log15.Register() // set log15 as the global logger with default settings
  dlog_logrus.Register() // set logrus as the global logger with default settings
  dlog_zap.Register() // set zap as the global logger with default settings
}

Or, do something more custom:

import (
  "os"

  "go.pedge.io/dlog"
  "go.pedge.io/dlog/logrus"

  "github.com/sirupsen/logrus"
)

func init() { // or anywhere
  logger := logrus.New()
  logger.Out = os.Stdout
  logger.Formatter = &logrus.TextFormatter{
    ForceColors: true,
  }
  dlog.SetLogger(dlog_logrus.NewLogger(logger))
}

By default, golang's standard logger is used. This is not recommended, however, as the implementation with the WithFields function is slow. It would be better to choose a different implementation in most cases.