/
entry.go
139 lines (112 loc) · 3.55 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package log
import (
"fmt"
"io/ioutil"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
"github.com/payshares/go/support/errors"
)
func (e *Entry) SetLevel(level logrus.Level) {
e.Logger.Level = level
}
// WithField creates a child logger annotated with the provided key value pair.
// A subsequent call to one of the logging methods (Debug(), Error(), etc.) to
// the return value from this function will cause the emitted log line to
// include the provided value.
func (e *Entry) WithField(key string, value interface{}) *Entry {
return &Entry{
Entry: *e.Entry.WithField(key, value),
}
}
// WithFields creates a child logger annotated with the provided key value
// pairs.
func (e *Entry) WithFields(fields F) *Entry {
return &Entry{
Entry: *e.Entry.WithFields(logrus.Fields(fields)),
}
}
// WithStack annotates this error with a stack trace from `stackProvider`, if
// available. normally `stackProvider` would be an error that implements
// `errors.StackTracer`.
func (e *Entry) WithStack(stackProvider interface{}) *Entry {
stack := "unknown"
if stackProvider, ok := stackProvider.(errors.StackTracer); ok {
stack = fmt.Sprint(stackProvider.StackTrace())
}
return e.WithField("stack", stack)
}
// Debugf logs a message at the debug severity.
func (e *Entry) Debugf(format string, args ...interface{}) {
e.Entry.Debugf(format, args...)
}
// Debug logs a message at the debug severity.
func (e *Entry) Debug(args ...interface{}) {
e.Entry.Debug(args...)
}
// Infof logs a message at the Info severity.
func (e *Entry) Infof(format string, args ...interface{}) {
e.Entry.Infof(format, args...)
}
// Info logs a message at the Info severity.
func (e *Entry) Info(args ...interface{}) {
e.Entry.Info(args...)
}
// Warnf logs a message at the Warn severity.
func (e *Entry) Warnf(format string, args ...interface{}) {
e.Entry.Warnf(format, args...)
}
// Warn logs a message at the Warn severity.
func (e *Entry) Warn(args ...interface{}) {
e.Entry.Warn(args...)
}
// Errorf logs a message at the Error severity.
func (e *Entry) Errorf(format string, args ...interface{}) {
e.Entry.Errorf(format, args...)
}
// Error logs a message at the Error severity.
func (e *Entry) Error(args ...interface{}) {
e.Entry.Error(args...)
}
// Panicf logs a message at the Panic severity.
func (e *Entry) Panicf(format string, args ...interface{}) {
e.Entry.Panicf(format, args...)
}
// Panic logs a message at the Panic severity.
func (e *Entry) Panic(args ...interface{}) {
e.Entry.Panic(args...)
}
// StartTest shifts this logger into "test" mode, ensuring that log lines will
// be recorded (rather than outputted). The returned function concludes the
// test, switches the logger back into normal mode and returns a slice of all
// raw logrus entries that were created during the test.
func (e *Entry) StartTest(level logrus.Level) func() []*logrus.Entry {
if e.isTesting {
panic("cannot start logger test: already testing")
}
e.isTesting = true
hook := &test.Hook{}
e.Logger.Hooks.Add(hook)
old := e.Logger.Out
e.Logger.Out = ioutil.Discard
oldLevel := e.Logger.Level
e.Logger.Level = level
return func() []*logrus.Entry {
e.Logger.Level = oldLevel
e.Logger.Out = old
e.removeHook(hook)
e.isTesting = false
return hook.Entries
}
}
// removeHook removes a hook, in the most complicated way possible.
func (e *Entry) removeHook(target logrus.Hook) {
for lvl, hooks := range e.Logger.Hooks {
kept := []logrus.Hook{}
for _, hook := range hooks {
if hook != target {
kept = append(kept, hook)
}
}
e.Logger.Hooks[lvl] = kept
}
}