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
- func AddHook(hook logrus.Hook)
- func AppendContext(ctx context.Context, fields KV) context.Context
- func Debug(args ...interface{})
- func Debugf(format string, args ...interface{})
- func Debugln(args ...interface{})
- func Error(args ...interface{})
- func Errorf(format string, args ...interface{})
- func Errorln(args ...interface{})
- func Fatal(args ...interface{})
- func Fatalf(format string, args ...interface{})
- func Fatalln(args ...interface{})
- func Info(args ...interface{})
- func Infof(format string, args ...interface{})
- func Infoln(args ...interface{})
- func Level() logrus.Level
- func NewContext(ctx context.Context) context.Context
- func Panic(args ...interface{})
- func Panicf(format string, args ...interface{})
- func Panicln(args ...interface{})
- func Print(args ...interface{})
- func Printf(format string, args ...interface{})
- func Println(args ...interface{})
- func SetFormatter(formatter logrus.Formatter)
- func SetLevel(level logrus.Level)
- func SetOutput(out io.Writer)
- func Warn(args ...interface{})
- func Warnf(format string, args ...interface{})
- func Warning(args ...interface{})
- func Warningf(format string, args ...interface{})
- func Warningln(args ...interface{})
- func Warnln(args ...interface{})
- type FieldLogger
- type KV
- type Logger
- type LoggerEntry
- func (c *LoggerEntry) AddHook(hook logrus.Hook)
- func (c *LoggerEntry) AppendContext(ctx context.Context, fields KV) context.Context
- func (c *LoggerEntry) Ctx(ctx context.Context) FieldLogger
- func (c *LoggerEntry) Err(err error) FieldLogger
- func (c *LoggerEntry) Fields() (fields KV)
- func (c *LoggerEntry) Level() (level logrus.Level)
- func (c *LoggerEntry) NewContext(ctx context.Context) context.Context
- func (c *LoggerEntry) SetFormatter(formatter logrus.Formatter)
- func (c *LoggerEntry) SetLevel(level logrus.Level)
- func (c *LoggerEntry) SetOutput(out io.Writer)
- func (c *LoggerEntry) With(fields KV) FieldLogger
Constants ¶
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 AppendContext ¶ added in v0.6.2
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 NewContext ¶ added in v0.4.0
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 ¶
SetFormatter sets the standard logger formatter.
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.
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
KV is a shorthand for logrus.Fields so less text is required to be typed:
log.With(log.KV{"k":"v"})
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
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
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