logger

package
v0.0.0-...-b8ff64e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 30, 2018 License: MIT Imports: 12 Imported by: 39

README

logger

logger is a library for writing structured json logs to stderr.

It is a wrapper around logrus that automatically formats as JSON and provides some default context (env, pid, host, and caller). It can capture output written to the default logger (since external dependencies will not be using this logger), and can add a rollbar rollbar hook to send all errors/panics/fatals to rollbar.

Usage

To use logger, just import this library, then call logger.Init(<level>) from your main or init. Then, you can log output using functions like logger.Infoln("message") and logger.Fatal("ohno"). See the logrus documentation for more functions you can use.

To use the rollbar logger, initialize with:

logger.InitWithRollbar(<level>, <token>, <env>)

Add this to main to send all top level panics to rollbar:

defer logger.LogPanic()

To capture and log panics in other goroutines, spawn them with

logger.Go( <func> )

When using logger.Go, be careful about variables in loops. Unlike normal goroutines, you can't pass arguments (including objects with methods called on them). As an example, to convert:

for i, o := range objects {
    go o.worker(i)
}

you will need to do one of the two following:

// Turn the loop variables into local function variables.
for i, o := range objects {
    logger.Go(func(o Obj, i int) func() {
        return func() { o.worker(i) }
    }(o, i))
}

or

// Alias the loop variables in the block scope.
for i, o := range objects {
    i := i
    o := o
    logger.Go(func() { o.worker(i) })
}

If you don't do one of the above, i and o will be shared across all goroutine invocations, which means that they will probably be the value of the last iteration of the loop.

And in your shutdown code, include logger.Wait() to wait for any remaining logs to be sent to rollbar before exiting.

Capturing default logger output

To capture output from the default logger, use logger.CaptureDefault(). To create a golang logger that will have its output forwarded to logger, use logger.GetCapturedLogger(). If you do these, don't provide additional configuration to the captured logger, or you may break the capturing.

Documentation

Overview

Package logger is a wrapper around logrus that logs in a structured JSON format and provides additional context keys. To use this library, first call `logger.Init("info")` or logger.InitWithRollbar(...), then call logging functions e.g. `logger.Infoln("message")`. You can also use CaptureDefault() to capture output that would go to the default logger (e.g. from dependencies).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureDefault

func CaptureDefault()

CaptureDefault causes all outupt to the default logger to be redirected to the logrus logger at the Warn level.

func Debug

func Debug(args ...interface{})

Debug logs the args at the Debug level.

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs the formatted string at Debug level.

func Debugln

func Debugln(args ...interface{})

Debugln logs the args at the Debug level.

func Error

func Error(args ...interface{})

Error logs the args at the Error level.

func Errorf

func Errorf(format string, args ...interface{})

Errorf logs the formatted string at Error level.

func Errorln

func Errorln(args ...interface{})

Errorln logs the args at the Error level.

func Fatal

func Fatal(args ...interface{})

Fatal logs the args at the Error level and exits.

func Fatalf

func Fatalf(format string, args ...interface{})

Fatalf logs the formatted string at Error level and exits.

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs the args at the Error level and exits.

func GetCapturedLogger

func GetCapturedLogger() *log.Logger

GetCapturedLogger returns a *log.Logger with its output redirected to the logrus logger at the Warn level.

func Go

func Go(f func())

Go spawns a goroutine like `go`, but captures and logs any panics that happen in them. Turn `go g()` into `logger.Go(func(){g()})

func Info

func Info(args ...interface{})

Info logs the args at the Info level.

func Infof

func Infof(format string, args ...interface{})

Infof logs the formatted string at Info level.

func Infoln

func Infoln(args ...interface{})

Infoln logs the args at the Info level.

func Init

func Init(level string)

Init sets up logging at the given level.

func InitLocal

func InitLocal(level string)

InitLocal sets up logging at the given level for local consumption.

func InitWithRollbar

func InitWithRollbar(level, rollbarToken, rollbarEnv string)

InitWithRollbar sets up logging at the given level, and sends log message of level panic, fatal, and error to rollbar. If rollbarToken isn't properly set, it will initialize the logrus logger without rollbar and move on, logging a message once the error response comes back from rollbar. In your main shutdown code, be sure to call rollbar.Wait() to guarantee late errors get sent to rollbar.

func LogPanic

func LogPanic()

LogPanic catches any raw panics and logs them. Triggers another panic.

func NewRollbarHook

func NewRollbarHook(token string, env string, levels []logrus.Level) logrus.Hook

NewRollbarHook creates a Rollbar hook for logrus.

func Panic

func Panic(args ...interface{})

Panic logs the args at the Error level and panics.

func Panicf

func Panicf(format string, args ...interface{})

Panicf logs the formatted string at Error level and panics.

func Panicln

func Panicln(args ...interface{})

Panicln logs the args at the Error level and panics.

func Print

func Print(args ...interface{})

Print logs the args at the Info level.

func Printf

func Printf(format string, args ...interface{})

Printf logs the formatted string at Info level.

func Println

func Println(args ...interface{})

Println logs the args at the Info level.

func Wait

func Wait()

Wait waits to finish sending logs to rollbar

func Warn

func Warn(args ...interface{})

Warn logs the args at the Warn level.

func Warnf

func Warnf(format string, args ...interface{})

Warnf logs the formatted string at Warn level.

func Warning

func Warning(args ...interface{})

Warning logs the args at the Warn level.

func Warningf

func Warningf(format string, args ...interface{})

Warningf logs the formatted string at Warn level.

func Warningln

func Warningln(args ...interface{})

Warningln logs the args at the Warn level.

func Warnln

func Warnln(args ...interface{})

Warnln logs the args at the Warn level.

Types

type Entry

type Entry struct {
	logrus.Entry
}

func WithError

func WithError(err error) *Entry

WithError adds the error to the `error` key.

func WithField

func WithField(key string, value interface{}) *Entry

WithField creates an Entry with the given key/value added.

func WithFields

func WithFields(fields map[string]interface{}) *Entry

WithFields creates an Entry with the given fields added.

func (*Entry) Debug

func (entry *Entry) Debug(args ...interface{})

Debug logs the args at the Debug level.

func (*Entry) Debugf

func (entry *Entry) Debugf(format string, args ...interface{})

Debugf logs the formatted string at Debug level.

func (*Entry) Debugln

func (entry *Entry) Debugln(args ...interface{})

Debugln logs the args at the Debug level.

func (*Entry) Error

func (entry *Entry) Error(args ...interface{})

Error logs the args at the Error level.

func (*Entry) Errorf

func (entry *Entry) Errorf(format string, args ...interface{})

Errorf logs the formatted string at Error level.

func (*Entry) Errorln

func (entry *Entry) Errorln(args ...interface{})

Errorln logs the args at the Error level.

func (*Entry) Fatal

func (entry *Entry) Fatal(args ...interface{})

Fatal logs the args at the Error level and exits.

func (*Entry) Fatalf

func (entry *Entry) Fatalf(format string, args ...interface{})

Fatalf logs the formatted string at Error level and exits.

func (*Entry) Fatalln

func (entry *Entry) Fatalln(args ...interface{})

Fatalln logs the args at the Error level and exits.

func (*Entry) Info

func (entry *Entry) Info(args ...interface{})

Info logs the args at the Info level.

func (*Entry) Infof

func (entry *Entry) Infof(format string, args ...interface{})

Infof logs the formatted string at Info level.

func (*Entry) Infoln

func (entry *Entry) Infoln(args ...interface{})

Infoln logs the args at the Info level.

func (*Entry) Panic

func (entry *Entry) Panic(args ...interface{})

Panic logs the args at the Error level and panics.

func (*Entry) Panicf

func (entry *Entry) Panicf(format string, args ...interface{})

Panicf logs the formatted string at Error level and panics.

func (*Entry) Panicln

func (entry *Entry) Panicln(args ...interface{})

Panicln logs the args at the Error level and panics.

func (*Entry) Print

func (entry *Entry) Print(args ...interface{})

Print logs the args at the Info level.

func (*Entry) Printf

func (entry *Entry) Printf(format string, args ...interface{})

Printf logs the formatted string at Info level.

func (*Entry) Println

func (entry *Entry) Println(args ...interface{})

Println logs the args at the Info level.

func (*Entry) Warn

func (entry *Entry) Warn(args ...interface{})

Warn logs the args at the Warn level.

func (*Entry) Warnf

func (entry *Entry) Warnf(format string, args ...interface{})

Warnf logs the formatted string at Warn level.

func (*Entry) Warning

func (entry *Entry) Warning(args ...interface{})

Warning logs the args at the Warn level.

func (*Entry) Warningf

func (entry *Entry) Warningf(format string, args ...interface{})

Warningf logs the formatted string at Warn level.

func (*Entry) Warningln

func (entry *Entry) Warningln(args ...interface{})

Warningln logs the args at the Warn level.

func (*Entry) Warnln

func (entry *Entry) Warnln(args ...interface{})

Warnln logs the args at the Warn level.

func (*Entry) WithError

func (entry *Entry) WithError(err error) *Entry

func (*Entry) WithField

func (entry *Entry) WithField(key string, value interface{}) *Entry

func (*Entry) WithFields

func (entry *Entry) WithFields(fields logrus.Fields) *Entry

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL