-
Notifications
You must be signed in to change notification settings - Fork 26
/
testLogger.go
44 lines (36 loc) · 958 Bytes
/
testLogger.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
package logging
import (
"io"
"github.com/go-kit/kit/log"
)
// testSink is implemented by testing.T and testing.B
type testSink interface {
Log(...interface{})
}
// testWriter implements io.Writer and delegates to a testSink
type testWriter struct {
testSink
}
func (t testWriter) Write(data []byte) (int, error) {
t.testSink.Log(string(data))
return len(data), nil
}
// NewTestWriter returns an io.Writer which delegates to a testing log.
// The returned io.Writer does not need to be synchronized.
func NewTestWriter(t testSink) io.Writer {
return testWriter{t}
}
// NewTestLogger produces a go-kit Logger which delegates to the supplied testing log.
func NewTestLogger(o *Options, t testSink) log.Logger {
if o == nil {
// we want to see all log output in tests by default
o = &Options{Level: "DEBUG"}
}
return NewFilter(
log.With(
o.loggerFactory()(NewTestWriter(t)),
TimestampKey(), log.DefaultTimestampUTC,
),
o,
)
}