-
Notifications
You must be signed in to change notification settings - Fork 11
/
logr_test.go
199 lines (163 loc) · 4.72 KB
/
logr_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
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
package logr_test
import (
"bufio"
"bytes"
"context"
"encoding/json"
"strings"
"testing"
"time"
"github.com/mattermost/logr/v2"
"github.com/mattermost/logr/v2/formatters"
"github.com/mattermost/logr/v2/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFlush(t *testing.T) {
buf := &bytes.Buffer{}
formatter := &formatters.Plain{DisableTimestamp: true, Delim: " | "}
filter := &logr.StdFilter{Lvl: logr.Info, Stacktrace: logr.Error}
target := test.NewSlowTarget(buf, 2)
lgr, _ := logr.New()
err := lgr.AddTarget(target, "flushTest", filter, formatter, 3000)
require.NoError(t, err)
cfg := test.DoSomeLoggingCfg{
Lgr: lgr,
Goroutines: 20,
Loops: 100,
Lvl: logr.Error,
}
test.DoSomeLogging(cfg)
logger := lgr.NewLogger().With(logr.String("node", "abcdefg"))
logger.Info("Last entry @!!@")
start := time.Now()
// blocks until flush is finished.
err = lgr.Flush()
require.NoError(t, err)
dur := time.Since(start)
t.Logf("Flush duration: %v", dur)
output := buf.String()
if !strings.Contains(output, "@!!@") {
t.Errorf("missing last log record")
}
// make sure logging can continue after flush.
test.DoSomeLogging(cfg)
logger.Info("Last entry %^^%")
// blocks until flush is finished.
err = lgr.Flush()
require.NoError(t, err)
output = buf.String()
if !strings.Contains(output, "%^^%") {
t.Errorf("missing last log record")
}
}
func TestFlushAfterShutdown(t *testing.T) {
buf := &bytes.Buffer{}
formatter := &formatters.Plain{DisableTimestamp: true, Delim: " | "}
filter := &logr.StdFilter{Lvl: logr.Info, Stacktrace: logr.Error}
target := test.NewSlowTarget(buf, 2)
lgr, _ := logr.New()
err := lgr.AddTarget(target, "flushTest", filter, formatter, 3000)
require.NoError(t, err)
cfg := test.DoSomeLoggingCfg{
Lgr: lgr,
Goroutines: 20,
Loops: 100,
Lvl: logr.Error,
}
test.DoSomeLogging(cfg)
err = lgr.Shutdown()
require.NoError(t, err)
// Should error since shutdown already called. Shouldn't crash.
err = lgr.Flush()
require.Error(t, err)
}
func TestLogAfterShutdown(t *testing.T) {
buf := &bytes.Buffer{}
formatter := &formatters.Plain{DisableTimestamp: true, Delim: " | "}
filter := &logr.StdFilter{Lvl: logr.Info, Stacktrace: logr.Error}
target := test.NewSlowTarget(buf, 2)
lgr, _ := logr.New()
err := lgr.AddTarget(target, "shutdownTest", filter, formatter, 3000)
require.NoError(t, err)
cfg := test.DoSomeLoggingCfg{
Lgr: lgr,
Goroutines: 20,
Loops: 100,
Lvl: logr.Error,
}
test.DoSomeLogging(cfg)
err = lgr.Shutdown()
require.NoError(t, err)
// Should NOP since shutdown already called. Shouldn't crash.
logger := lgr.NewLogger().With(logr.String("test", "yes"))
logger.Info("This shouldn't get logged")
// Second shutdown should error, but not crash.
err = lgr.Shutdown()
require.Error(t, err)
output := buf.String()
if strings.Contains(output, "This shouldn't get logged") {
t.Errorf("log record should not appear after shutdown")
}
}
func TestRemoveTarget(t *testing.T) {
formatter := &formatters.Plain{DisableTimestamp: true, Delim: " | "}
filter := &logr.StdFilter{Lvl: logr.Info, Stacktrace: logr.Error}
lgr, _ := logr.New()
buf1 := &bytes.Buffer{}
target1 := test.NewSlowTarget(buf1, 2)
err := lgr.AddTarget(target1, "t1", filter, formatter, 3000)
require.NoError(t, err)
buf2 := &bytes.Buffer{}
target2 := test.NewSlowTarget(buf2, 2)
err = lgr.AddTarget(target2, "t2", filter, formatter, 3000)
require.NoError(t, err)
cfg := test.DoSomeLoggingCfg{
Lgr: lgr,
Goroutines: 20,
Loops: 100,
Lvl: logr.Error,
}
test.DoSomeLogging(cfg)
err = lgr.RemoveTargets(context.Background(), func(ti logr.TargetInfo) bool {
return ti.Name == "t2"
})
assert.NoError(t, err)
tarr := lgr.TargetInfos()
assert.Len(t, tarr, 1)
assert.Equal(t, tarr[0].Name, "t1")
err = lgr.Shutdown()
if err != nil {
t.Error(err)
}
}
func TestLimitLogFields(t *testing.T) {
formatter := &formatters.JSON{DisableTimestamp: true}
filter := &logr.StdFilter{Lvl: logr.Info, Stacktrace: logr.Error}
lgr, _ := logr.New(logr.MaxFieldLen(8))
buf := &bytes.Buffer{}
target := test.NewSlowTarget(buf, 2)
err := lgr.AddTarget(target, "t", filter, formatter, 3000)
require.NoError(t, err)
cfg := test.DoSomeLoggingCfg{
Lgr: lgr,
Goroutines: 1,
Loops: 1,
Lvl: logr.Info,
}
test.DoSomeLogging(cfg)
err = lgr.Flush()
require.NoError(t, err)
scanner := bufio.NewScanner(buf)
entry := struct {
Message string `json:"msg"`
}{}
var numLines int
for scanner.Scan() {
err = json.Unmarshal(scanner.Bytes(), &entry)
require.NoError(t, err)
require.Equal(t, "This is ...", entry.Message)
numLines++
}
require.Equal(t, 1, numLines)
}