forked from fluxio/logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
std_logger_test.go
134 lines (118 loc) · 3.65 KB
/
std_logger_test.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
package logging
import (
"bytes"
"testing"
"time"
. "github.com/smartystreets/goconvey/convey"
)
func args(v ...interface{}) []interface{} { return v }
type captureWriter Entry
func (c *captureWriter) Write(e Entry) error { *c = captureWriter(e); return nil }
func TestStdLogger(t *testing.T) {
Convey("Standard Logger", t, func() {
var c captureWriter
log := StdLogger{"test", &c, TraceLevel}
Convey("should capture the logging time", func() {
t0 := time.Now()
log.Info("")
t1 := time.Now()
So(c.Time, ShouldHappenOnOrBetween, t0, t1)
})
Convey("should capture the logging args", func() {
log.Debugf("asdf", 1, 2, "x", log)
So(c.Fmt, ShouldEqual, "asdf")
So(c.Args, ShouldResemble, []interface{}{1, 2, "x", log})
})
Convey("should capture the context", func() {
log.Info("")
So(c.Context, ShouldEqual, "test")
})
Convey("should capture the log level", func() {
log.Debug("")
So(c.Level, ShouldEqual, DebugLevel)
log.Info("")
So(c.Level, ShouldEqual, InfoLevel)
log.Error("")
So(c.Level, ShouldEqual, ErrorLevel)
log.Trace("")
So(c.Level, ShouldEqual, TraceLevel)
})
Convey("should accurately log the call point", func() {
log.Info("hi")
So(c.File, ShouldContainSubstring, "std_logger_test.go")
})
Convey("should filter out log levels", func() {
log.Tracef("a")
So(c.Fmt, ShouldEqual, "a")
log.MinLevel = InfoLevel
log.Tracef("b")
So(c.Fmt, ShouldEqual, "a") // unchanged
log.Debugf("c")
So(c.Fmt, ShouldEqual, "a") // unchanged
log.Infof("d")
So(c.Fmt, ShouldEqual, "d") // picked it up!
log.Errorf("e")
So(c.Fmt, ShouldEqual, "e") // picked it up!
})
Convey("should allow change of log level", func() {
log.Tracef("a")
So(c.Fmt, ShouldEqual, "a")
log.SetLogLevel(InfoLevel)
log.Tracef("b")
So(c.Fmt, ShouldEqual, "a") // unchanged
log.Debugf("c")
So(c.Fmt, ShouldEqual, "a") // unchanged
log.Infof("d")
So(c.Fmt, ShouldEqual, "d") // picked it up!
log.Errorf("e")
So(c.Fmt, ShouldEqual, "e") // picked it up!
So(log.LogLevel(), ShouldEqual, InfoLevel)
})
})
}
func TestNewTextLogger(t *testing.T) {
Convey("NewTextLogger", t, func() {
Convey("should make Loggers", func() {
var _ Logger = NewTextLogger(nil, "ctx", TraceLevel)
})
Convey("write to the provided io.Writer", func() {
var buf bytes.Buffer
var log Logger = NewTextLogger(&buf, "Flow=f1", TraceLevel)
log.Infof("Creating %d blocks", 20)
So(buf.String(), ShouldContainSubstring, "Flow=f1")
So(buf.String(), ShouldContainSubstring, "Creating 20 blocks")
})
Convey("and set the min level", func() {
var buf bytes.Buffer
log := NewTextLogger(&buf, "ctx", DebugLevel)
log.Trace("xxx")
log.Debug("yyy")
log.Info("zzz")
So(buf.String(), ShouldNotContainSubstring, "xxx")
So(buf.String(), ShouldContainSubstring, "yyy")
So(buf.String(), ShouldContainSubstring, "zzz")
})
})
}
func TestSystemLogger(t *testing.T) {
var c captureWriter
var saved Logger
System, saved = &StdLogger{"fake system", &c, TraceLevel}, System
defer func() { System = saved }()
Convey("The global system logger", t, func() {
Convey("should accurately log the call point", func() {
Info("hi there")
So(c.File, ShouldContainSubstring, "std_logger_test.go")
})
})
}
func TestWrappedLoggerTracing(t *testing.T) {
Convey("Wrapped loggers should report a useful stack frame", t, func() {
buf := &bytes.Buffer{}
wrapped := CancellableLogger{Logger: NewTextLogger(buf, "dummy", TraceLevel)}
wrapped.Trace("hello")
s := buf.String()
So(s, ShouldNotContainSubstring, "cancellable_logger.go")
So(s, ShouldContainSubstring, "std_logger_test.go")
})
}