Documentation
¶
Overview ¶
Package errfield adds possibility to wrap errors with fields and then log them in structured way.
Example ¶
package main import ( "errors" "os" log "github.com/sirupsen/logrus" "github.com/go-logrusutil/logrusutil/errfield" ) func main() { log.SetOutput(os.Stdout) // setup the errfield.Formatter log.SetFormatter(&errfield.Formatter{ Formatter: &log.TextFormatter{DisableTimestamp: true}, }) // use errfield.Add to add fields err := errors.New("something failed") err = errfield.Add(err, "foo", "bar") log.WithError(err).Error("crash") }
Output: level=error msg=crash error="something failed" foo=bar
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Formatter ¶
type Formatter struct { // Formatter is the decorated logrus.Formatter. // Default TextFormatter is used when none provided. logrus.Formatter // ErrorFieldsKey defines under which key the error log fields would be added. // For empty string it des not create any dedicated key. ErrorFieldsKey string }
Formatter decorates logrus.Formatter to add error fields under to the log entry. Implements logrus.Formatter.
Example (ErrorFieldsKey) ¶
package main import ( "errors" "os" log "github.com/sirupsen/logrus" "github.com/go-logrusutil/logrusutil/errfield" ) func main() { log.SetOutput(os.Stdout) // setup the errfield.Formatter with ErrorFieldsKey log.SetFormatter(&errfield.Formatter{ Formatter: &log.TextFormatter{DisableTimestamp: true}, ErrorFieldsKey: "error_fields", }) // use errfield.Add to add fields err := errors.New("something failed") err = errfield.Add(err, "fizz", "buzz") log.WithError(err).Error("crash") }
Output: level=error msg=crash error="something failed" error_fields="map[fizz:buzz]"
Click to show internal directories.
Click to hide internal directories.