-
Notifications
You must be signed in to change notification settings - Fork 350
/
logger.go
237 lines (202 loc) · 4.6 KB
/
logger.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package loggingtest
import (
"errors"
"fmt"
"log"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
)
type logSubscription struct {
exp string
n int
notify chan<- struct{}
}
type countMessage struct {
expression string
response chan<- int
}
type logWatch struct {
entries []string
reqs []*logSubscription
}
// Logger provides an implementation of the logging.Logger interface
// that can be used to receive notifications about log events.
type Logger struct {
logc chan string
notify chan<- logSubscription
count chan countMessage
clear chan chan struct{}
mute chan bool
quit chan struct{}
}
// ErrWaitTimeout is returned when a logging event doesn't happen
// within a timeout.
var ErrWaitTimeout = errors.New("timeout")
func (lw *logWatch) save(e string) {
lw.entries = append(lw.entries, e)
for i := len(lw.reqs) - 1; i >= 0; i-- {
req := lw.reqs[i]
if strings.Contains(e, req.exp) {
req.n--
if req.n <= 0 {
close(req.notify)
lw.reqs = append(lw.reqs[:i], lw.reqs[i+1:]...)
}
}
}
}
func (lw *logWatch) notify(req logSubscription) {
for i := len(lw.entries) - 1; i >= 0; i-- {
if strings.Contains(lw.entries[i], req.exp) {
req.n--
if req.n == 0 {
break
}
}
}
if req.n <= 0 {
close(req.notify)
} else {
lw.reqs = append(lw.reqs, &req)
}
}
func (lw *logWatch) count(m countMessage) {
var count int
for _, e := range lw.entries {
if strings.Contains(e, m.expression) {
count++
}
}
m.response <- count
}
func (lw *logWatch) clear() {
lw.entries = nil
lw.reqs = nil
}
// Returns a new, initialized instance of Logger.
func New() *Logger {
lw := &logWatch{}
logc := make(chan string)
notify := make(chan logSubscription)
count := make(chan countMessage)
clear := make(chan chan struct{})
mute := make(chan bool)
quit := make(chan struct{})
muted := !testing.Verbose()
tl := &Logger{
logc: logc,
notify: notify,
count: count,
clear: clear,
mute: mute,
quit: quit,
}
go func() {
for {
select {
case e := <-logc:
lw.save(e)
if !muted {
log.Println(e)
}
case req := <-notify:
lw.notify(req)
case req := <-count:
lw.count(req)
case c := <-clear:
lw.clear()
c <- struct{}{}
case m := <-mute:
muted = m
case <-quit:
return
}
}
}()
return tl
}
func (tl *Logger) logf(f string, a ...interface{}) {
select {
case tl.logc <- fmt.Sprintf(f, a...):
case <-tl.quit:
}
}
func (tl *Logger) log(a ...interface{}) {
select {
case tl.logc <- fmt.Sprint(a...):
case <-tl.quit:
}
}
// Returns nil when n logging events matching exp were received or returns
// ErrWaitTimeout when to timeout expired.
func (tl *Logger) WaitForN(exp string, n int, to time.Duration) error {
found := make(chan struct{}, 1)
tl.notify <- logSubscription{exp, n, found}
select {
case <-found:
return nil
case <-time.After(to):
return ErrWaitTimeout
}
}
// Returns nil when a logging event matching exp was received or returns
// ErrWaitTimeout when to timeout expired.
func (tl *Logger) WaitFor(exp string, to time.Duration) error {
return tl.WaitForN(exp, 1, to)
}
// Count returns the recorded messages that match exp.
func (tl *Logger) Count(expression string) int {
rsp := make(chan int)
m := countMessage{
expression: expression,
response: rsp,
}
tl.count <- m
return <-rsp
}
// Clears the stored logging events.
func (tl *Logger) Reset() {
ch := make(chan struct{})
tl.clear <- ch
<-ch
}
func (tl *Logger) Mute() {
tl.mute <- true
}
func (tl *Logger) Unmute() {
tl.mute <- false
}
// Closes the logger.
func (tl *Logger) Close() {
close(tl.quit)
}
func (tl *Logger) Error(a ...interface{}) { tl.log(a...) }
func (tl *Logger) Errorf(f string, a ...interface{}) { tl.logf(f, a...) }
func (tl *Logger) Warn(a ...interface{}) { tl.log(a...) }
func (tl *Logger) Warnf(f string, a ...interface{}) { tl.logf(f, a...) }
func (tl *Logger) Info(a ...interface{}) { tl.log(a...) }
func (tl *Logger) Infof(f string, a ...interface{}) { tl.logf(f, a...) }
func (tl *Logger) Debug(a ...interface{}) { tl.log(a...) }
func (tl *Logger) Debugf(f string, a ...interface{}) { tl.logf(f, a...) }
func (tl *Logger) Fire(entry *logrus.Entry) error {
line, err := entry.String()
if err != nil {
return err
}
switch entry.Level {
case logrus.PanicLevel, logrus.FatalLevel, logrus.ErrorLevel:
tl.Error(line)
case logrus.WarnLevel:
tl.Warn(line)
case logrus.InfoLevel:
tl.Info(line)
case logrus.DebugLevel, logrus.TraceLevel:
tl.Debug(line)
}
return nil
}
func (tl *Logger) Levels() []logrus.Level {
return logrus.AllLevels
}