...
1
2 package logrushandler
3
4 import (
5 "github.com/sirupsen/logrus"
6
7 "github.com/goph/emperror"
8 "github.com/goph/emperror/internal/keyvals"
9 )
10
11
12 type Handler struct {
13 logger logrus.FieldLogger
14 }
15
16
17 func New(logger logrus.FieldLogger) *Handler {
18 return &Handler{
19 logger: logger,
20 }
21 }
22
23
24 func (h *Handler) Handle(err error) {
25 logger := h.logger
26
27
28 if kvs := emperror.Context(err); len(kvs) > 0 {
29 logger = h.logger.WithFields(logrus.Fields(keyvals.ToMap(kvs)))
30 }
31
32 type errorCollection interface {
33 Errors() []error
34 }
35
36 if errs, ok := err.(errorCollection); ok {
37 for _, e := range errs.Errors() {
38 logger.WithField("parent", err.Error()).Error(e.Error())
39 }
40 } else {
41 logger.Error(err.Error())
42 }
43 }
44
View as plain text