log

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2016 License: MIT Imports: 4 Imported by: 5

Documentation

Overview

Package log provides some helpers for structured contextual logging

Handle global logging with context. Based on logrus(https://github.com/Sirupsen/logrus) with an option to create a global context

package (
    "github.com/graze/golang-service/log"
    "github.com/Sirupsen/logrus"
)

Global properties that are used whenever `log.<xxx>` is called can be set as such:

log.SetFormatter(&logrus.TextFormatter{})
log.SetOutput(os.Stderr)
log.SetLevel(log.InfoLevel)
log.Add(log.KV{"service":"super_service"}) // apply `service=super_service` to each log message

You can then log messages using the `log.` commands which will use the above configuration

log.Add(log.KV{
    "app": "http-service"
})

log.With(log.KV{
    "module": "request_handler",
    "tag":    "received_request"
    "method": "GET",
    "path":   "/path"
}).Info("Received request");

// app="http-service" module=request_handler tag=received_request method=GET path="/path" level=info
//   msg="Received request"

It is also possible to create a new logger ignoring the global configuration set above. Calling `log.New` will create a new instance of a logger which can be passed around and used with other methods

// create a fresh context using defaults (ignores the global logger properties set above)
logger := log.New()
logger.Add(log.KV{
    "module": "request_handler"
})
logger.With(log.KV{
    "tag":    "received_request",
    "method": "GET",
    "path":   "/path"
}).Info("Recieved GET /path")
logger.Err(err).Error("Failed to handle input request")

// module=request_handler method=GET tag=received_request path="/path" level=error err="some error"
//   msg="Failed to handler input request"

When a new logger is created, the format and output can be modified to change the how messages passed to this logger are logged

logger := log.New()
logger.SetFormatter(&logrus.JSONFormatter{})
logger.SetLevel(log.DebugLevel)
logger.SetOutput(os.Stdout)

logger.Debug("some debug output printed")

// level=debug msg="some debug output printed"

This logger supports golang's `Context`. You can create a new context and use an existing context as such

logger := log.New()
logger.Add(log.KV{"key":"value"})
ctx := logger.NewContext(context.Background())

log.Ctx(ctx).Info("text")

// key=value level=info msg=text

You can use a logging context stored within a `context.Context` with a second local logger

logger := log.New()
logger.Add(log.KV{"key":"value"})
ctx := logger.NewContext(context.Background())

logger2 := log.New()
logger2.Add(log.KV{"key2":"value2"})
logger2.Ctx(ctx).Info("text")

// key=value key2=value2 level=info msg=text

As the logger is based on logrus you can add Hooks to each logger to send data to multiple outputs. See: https://github.com/Sirupsen/logrus#hooks

Index

Constants

View Source
const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel logrus.Level = iota
	// FatalLevel level. Logs and then calls `os.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
)

These are the different logging levels. You can set the logging level to log on your instance of logger, obtained with `logrus.New()`.

Variables

This section is empty.

Functions

func AddHook

func AddHook(hook logrus.Hook)

AddHook adds a new hook to the global logging context

func AppendContext added in v0.6.2

func AppendContext(ctx context.Context, fields KV) context.Context

AppendContext creates a new context.Context from the supplied ctx with the fields appended to the end

func Debug

func Debug(args ...interface{})

Debug logs a message at level Debug on the standard logger.

func Debugf

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

Debugf logs a message at level Debug on the standard logger.

func Debugln added in v0.8.0

func Debugln(args ...interface{})

Debugln logs a message at level Debug on the standard logger.

func Error

func Error(args ...interface{})

Error logs a message at level Error on the standard logger.

func Errorf

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

Errorf logs a message at level Error on the standard logger.

func Errorln added in v0.8.0

func Errorln(args ...interface{})

Errorln logs a message at level Error on the standard logger.

func Fatal added in v0.8.0

func Fatal(args ...interface{})

Fatal logs a message at level Fatal on the standard logger

func Fatalf added in v0.8.0

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

Fatalf logs a message at level Fatal on the standard logger

func Fatalln added in v0.8.0

func Fatalln(args ...interface{})

Fatalln logs a message at level Fatal on the standard logger

func Info

func Info(args ...interface{})

Info logs a message at level Info on the standard logger.

func Infof

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

Infof logs a message at level Info on the standard logger.

func Infoln added in v0.8.0

func Infoln(args ...interface{})

Infoln logs a message at level Info on the standard logger.

func Level added in v0.4.0

func Level() logrus.Level

Level returns the standard logger level.

func NewContext added in v0.4.0

func NewContext(ctx context.Context) context.Context

NewContext adds the current `logEntry` into `ctx`

func Panic added in v0.8.0

func Panic(args ...interface{})

Panic logs a message at level Panic on the standard logger

func Panicf added in v0.8.0

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

Panicf logs a message at level Panic on the standard logger

func Panicln added in v0.8.0

func Panicln(args ...interface{})

Panicln logs a message at level Panic on the standard logger

func Print

func Print(args ...interface{})

Print logs a message at level Info on the standard logger.

func Printf

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

Printf logs a message at level Info on the standard logger.

func Println added in v0.8.0

func Println(args ...interface{})

Println logs a message at level Info on the standard logger.

func SetFormatter

func SetFormatter(formatter logrus.Formatter)

SetFormatter sets the standard logger formatter.

func SetLevel

func SetLevel(level logrus.Level)

SetLevel sets the standard logger level.

func SetOutput

func SetOutput(out io.Writer)

SetOutput sets the standard logger output.

func Warn added in v0.8.0

func Warn(args ...interface{})

Warn logs a message at level Warning on the standard logger.

func Warnf added in v0.8.0

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

Warnf logs a message at level Warning on the standard logger.

func Warning added in v0.8.0

func Warning(args ...interface{})

Warning logs a message at level Warning on the standard logger.

func Warningf added in v0.8.0

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

Warningf logs a message at level Warning on the standard logger.

func Warningln added in v0.8.0

func Warningln(args ...interface{})

Warningln logs a message at level Warning on the standard logger.

func Warnln added in v0.8.0

func Warnln(args ...interface{})

Warnln logs a message at level Warning on the standard logger.

Types

type FieldLogger added in v0.6.0

type FieldLogger interface {
	NewContext(ctx context.Context) context.Context
	AppendContext(ctx context.Context, fields KV) context.Context

	Ctx(ctx context.Context) FieldLogger
	With(fields KV) FieldLogger
	Err(err error) FieldLogger

	Fields() KV

	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Printf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Warningf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
	Panicf(format string, args ...interface{})

	Debug(args ...interface{})
	Info(args ...interface{})
	Print(args ...interface{})
	Warn(args ...interface{})
	Warning(args ...interface{})
	Error(args ...interface{})
	Fatal(args ...interface{})
	Panic(args ...interface{})

	Debugln(args ...interface{})
	Infoln(args ...interface{})
	Println(args ...interface{})
	Warnln(args ...interface{})
	Warningln(args ...interface{})
	Errorln(args ...interface{})
	Fatalln(args ...interface{})
	Panicln(args ...interface{})
}

FieldLogger represents a Logging FieldLogger

func Ctx added in v0.4.0

func Ctx(ctx context.Context) FieldLogger

Ctx will use the provided context with its logs if applicable

func Err

func Err(err error) FieldLogger

Err creates a new LoggerEntry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.

func With

func With(fields KV) FieldLogger

With returns a new LoggerEntry with the supplied fields

type KV added in v0.4.0

type KV logrus.Fields

KV is a shorthand for logrus.Fields so less text is required to be typed:

log.With(log.KV{"k":"v"})

func Fields added in v0.4.0

func Fields() KV

Fields will return the current set of fields in the global context

type Logger added in v0.3.0

type Logger interface {
	SetOutput(out io.Writer)
	SetLevel(level logrus.Level)
	Level() logrus.Level
	SetFormatter(formatter logrus.Formatter)
	AddHook(hook logrus.Hook)
}

Logger represents a struct that can modify the output of a log

type LoggerEntry added in v0.6.0

type LoggerEntry struct {
	*logrus.Entry
}

LoggerEntry is a logging context that can be passed around

func New

func New() (entry *LoggerEntry)

New creates a new FieldLogger with a new Logger (formatter, level, output, hooks)

func (*LoggerEntry) AddHook added in v0.6.0

func (c *LoggerEntry) AddHook(hook logrus.Hook)

AddHook will add a hook to the current context

func (*LoggerEntry) AppendContext added in v0.6.2

func (c *LoggerEntry) AppendContext(ctx context.Context, fields KV) context.Context

AppendContext will create a new context.Context based on ctx with the fields appended

func (*LoggerEntry) Ctx added in v0.6.0

func (c *LoggerEntry) Ctx(ctx context.Context) FieldLogger

Ctx will use any logging context contained in context.Context to append to the current log context

func (*LoggerEntry) Err added in v0.6.0

func (c *LoggerEntry) Err(err error) FieldLogger

Err adds an error and returns a new LoggerEntry

func (*LoggerEntry) Fields added in v0.6.0

func (c *LoggerEntry) Fields() (fields KV)

Fields will return the current fields attached to a context

func (*LoggerEntry) Level added in v0.6.0

func (c *LoggerEntry) Level() (level logrus.Level)

Level returns the current logging level this context will log at

func (*LoggerEntry) NewContext added in v0.6.0

func (c *LoggerEntry) NewContext(ctx context.Context) context.Context

NewContext returns the provided context with this LoggerEntry added

func (*LoggerEntry) SetFormatter added in v0.6.0

func (c *LoggerEntry) SetFormatter(formatter logrus.Formatter)

SetFormatter will change the formatter for the current context

func (*LoggerEntry) SetLevel added in v0.6.0

func (c *LoggerEntry) SetLevel(level logrus.Level)

SetLevel changes the default logging level of the current context

func (*LoggerEntry) SetOutput added in v0.6.0

func (c *LoggerEntry) SetOutput(out io.Writer)

SetOutput changes the output of the current context

func (*LoggerEntry) With added in v0.6.0

func (c *LoggerEntry) With(fields KV) FieldLogger

With creates a new LoggerEntry and adds the fields to it

Jump to

Keyboard shortcuts

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