From 8c292d9e6bc9e34887e0ae759bde2a1d35790c52 Mon Sep 17 00:00:00 2001 From: Danny Kopping Date: Tue, 5 Jul 2022 07:27:30 +0200 Subject: [PATCH] Log wrapped errors Signed-off-by: Danny Kopping --- logger.go | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/logger.go b/logger.go index d2e5299..164bcb4 100644 --- a/logger.go +++ b/logger.go @@ -1,6 +1,7 @@ package errata import ( + "errors" "os" "github.com/go-kit/log" @@ -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 { @@ -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{} {