Skip to content

Commit

Permalink
Log wrapped errors
Browse files Browse the repository at this point in the history
Signed-off-by: Danny Kopping <[email protected]>
  • Loading branch information
Danny Kopping committed Jul 5, 2022
1 parent a4c27b2 commit 8c292d9
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errata

import (
"errors"
"os"

"github.com/go-kit/log"
Expand All @@ -18,7 +19,7 @@ func init() {
logger = log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
}

func LogError(err error) {
func LogError(err error, args ...interface{}) {
lvl := level.ErrorValue()

if e, ok := err.(LogLevel); ok {
Expand All @@ -28,17 +29,27 @@ func LogError(err error) {

// create a temporary logger so we don't overwrite the global logger
var l log.Logger
l = log.With(logger, args...)

var parentUUID string

if e, ok := err.(Erratum); ok {
parentUUID = e.UUID()

// see if the error we received implements the Erratum interface, and enhance the output with that data
l = log.With(logger, "code", e.Code(), "msg", e.Message(), "uuid", e.UUID(), "help", e.HelpURL())
l = log.With(l, "code", e.Code(), "err", e.Error(), "uuid", e.UUID(), "help", e.HelpURL())
// add arguments if any have been defined
l = log.With(l, argsToKeyVals(e.Args())...)
} else {
// if this is not an Erratum, just use the error message
l = log.With(logger, "err", err.Error())
l = log.With(l, "err", err.Error())
}

log.WithPrefix(l, level.Key(), lvl).Log()

if unwrapped := errors.Unwrap(err); unwrapped != nil {
LogError(unwrapped, "parent-uuid", parentUUID)
}
}

func argsToKeyVals(args map[string]interface{}) []interface{} {
Expand Down

0 comments on commit 8c292d9

Please sign in to comment.