forked from gomods/athens
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
66 lines (56 loc) · 1.44 KB
/
entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package log
import (
"github.com/gomods/athens/pkg/errors"
"github.com/sirupsen/logrus"
)
// Entry is an abstraction to the
// Logger and the logrus.Entry
// so that *Logger always creates
// an Entry copy which ensures no
// Fields are being overwritten.
type Entry interface {
// Basic Logging Operation
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
// Attach contextual information to the logging entry
WithFields(fields map[string]interface{}) Entry
// SystemErr is a method that disects the error
// and logs the appropriate level and fields for it.
SystemErr(err error)
}
type entry struct {
*logrus.Entry
}
func (e *entry) WithFields(fields map[string]interface{}) Entry {
ent := e.Entry.WithFields(fields)
return &entry{ent}
}
func (e *entry) SystemErr(err error) {
athensErr, ok := err.(errors.Error)
if !ok {
e.Error(err)
return
}
ent := e.WithFields(errFields(athensErr))
switch errors.Severity(err) {
case logrus.WarnLevel:
ent.Warnf("%v", err)
case logrus.InfoLevel:
ent.Infof("%v", err)
case logrus.DebugLevel:
ent.Debugf("%v", err)
default:
ent.Errorf("%v", err)
}
}
func errFields(err errors.Error) logrus.Fields {
f := logrus.Fields{}
f["operation"] = err.Op
f["kind"] = errors.KindText(err)
f["module"] = err.Module
f["version"] = err.Version
f["ops"] = errors.Ops(err)
return f
}