-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogger_test.go
291 lines (254 loc) · 6.21 KB
/
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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package logger_test
import (
"bytes"
"errors"
"io"
"runtime"
"strconv"
"testing"
"time"
"github.com/hamba/logger/v2"
"github.com/hamba/logger/v2/ctx"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/trace"
)
func TestLevelFromString(t *testing.T) {
tests := []struct {
lvl string
want logger.Level
wantError bool
}{
{
lvl: "trce",
want: logger.Trace,
wantError: false,
},
{
lvl: "trace",
want: logger.Trace,
wantError: false,
},
{
lvl: "dbug",
want: logger.Debug,
wantError: false,
},
{
lvl: "debug",
want: logger.Debug,
wantError: false,
},
{
lvl: "info",
want: logger.Info,
wantError: false,
},
{
lvl: "warn",
want: logger.Warn,
wantError: false,
},
{
lvl: "eror",
want: logger.Error,
wantError: false,
},
{
lvl: "error",
want: logger.Error,
wantError: false,
},
{
lvl: "crit",
want: logger.Crit,
wantError: false,
},
{
lvl: "unkn",
want: logger.Level(123),
wantError: true,
},
}
for _, tt := range tests {
t.Run(tt.lvl, func(t *testing.T) {
lvl, err := logger.LevelFromString(tt.lvl)
if tt.wantError {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.want, lvl)
})
}
}
func TestLevel_String(t *testing.T) {
tests := []struct {
lvl logger.Level
want string
}{
{
lvl: logger.Debug,
want: "dbug",
},
{
lvl: logger.Info,
want: "info",
},
{
lvl: logger.Warn,
want: "warn",
},
{
lvl: logger.Error,
want: "eror",
},
{
lvl: logger.Crit,
want: "crit",
},
{
lvl: logger.Level(123),
want: "unkn",
},
}
for _, tt := range tests {
assert.Equal(t, tt.want, tt.lvl.String())
}
}
func TestNew(t *testing.T) {
log := logger.New(io.Discard, logger.LogfmtFormat(), logger.Debug)
assert.IsType(t, &logger.Logger{}, log)
}
func TestLogger(t *testing.T) {
tests := []struct {
name string
fn func(l *logger.Logger)
want string
}{
{
name: "Trace",
fn: func(l *logger.Logger) { l.Trace("debug", ctx.Str("level", "trace")) },
want: "lvl=trce msg=debug level=trace\n",
},
{
name: "Debug",
fn: func(l *logger.Logger) { l.Debug("debug", ctx.Str("level", "debug")) },
want: "lvl=dbug msg=debug level=debug\n",
},
{
name: "Info",
fn: func(l *logger.Logger) { l.Info("info", ctx.Str("level", "info")) },
want: "lvl=info msg=info level=info\n",
},
{
name: "Warn",
fn: func(l *logger.Logger) { l.Warn("warn", ctx.Str("level", "warn")) },
want: "lvl=warn msg=warn level=warn\n",
},
{
name: "Error",
fn: func(l *logger.Logger) { l.Error("error", ctx.Str("level", "error")) },
want: "lvl=eror msg=error level=error\n",
},
{
name: "Crit",
fn: func(l *logger.Logger) { l.Crit("critical", ctx.Str("level", "critical")) },
want: "lvl=crit msg=critical level=critical\n",
},
}
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Trace)
test.fn(log)
assert.Equal(t, test.want, buf.String())
})
}
}
func TestLogger_DiscardsLogs(t *testing.T) {
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Error)
log.Debug("some message")
assert.Equal(t, "", buf.String())
}
func TestLogger_Context(t *testing.T) {
obj := struct {
Name string
}{Name: "test"}
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info).With(ctx.Str("_n", "bench"), ctx.Int("_p", 1))
span := &fakeSpan{ID: byte(2), Recording: true}
_, file, line, _ := runtime.Caller(0)
caller := file + ":" + strconv.Itoa(line+3)
log.Info("some message",
ctx.Str("str", "string"),
ctx.Strs("strs", []string{"string1", "string2"}),
ctx.Bytes("bytes", []byte("bytes")),
ctx.Bool("bool", true),
ctx.Int("int", 1),
ctx.Ints("ints", []int{1, 2, 3}),
ctx.Int8("int8", 2),
ctx.Int16("int16", 3),
ctx.Int32("int32", 4),
ctx.Int64("int64", 5),
ctx.Uint("uint", 1),
ctx.Uint8("uint8", 2),
ctx.Uint16("uint16", 3),
ctx.Uint32("uint32", 4),
ctx.Uint64("uint64", 5),
ctx.Float32("float32", 1.23),
ctx.Float64("float64", 4.56),
ctx.Error("err", errors.New("test error")),
ctx.Err(errors.New("test error")),
ctx.Time("time", time.Unix(1541573670, 0).UTC()),
ctx.Duration("dur", time.Second),
ctx.Interface("obj", obj),
ctx.Caller("caller"),
ctx.TraceID("tid", span),
)
want := `lvl=info msg="some message" _n=bench _p=1 str=string strs=string1,string2 bytes=98,121,116,101,115 bool=true int=1 ints=1,2,3 int8=2 int16=3 int32=4 int64=5 uint=1 uint8=2 uint16=3 uint32=4 uint64=5 float32=1.230 float64=4.560 err="test error" error="test error" time=1541573670 dur=1s obj={Name:test} caller=` + caller + " tid=01000000000000000000000000000000\n"
assert.Equal(t, want, buf.String())
}
func TestLogger_Stack(t *testing.T) {
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info)
log.Info("some message", ctx.Stack("stack"))
want := `lvl=info msg="some message" stack=[github.com/hamba/logger/logger/logger_test.go:245]` + "\n"
assert.Equal(t, want, buf.String())
}
func TestLogger_Timestamp(t *testing.T) {
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info)
cancel := log.WithTimestamp()
defer cancel()
log.Info("some message")
want := `^ts=\d+ lvl=info msg="some message"` + "\n$"
assert.Regexp(t, want, buf.String())
}
func TestLogger_Writer(t *testing.T) {
var buf bytes.Buffer
log := logger.New(&buf, logger.LogfmtFormat(), logger.Info)
w := log.Writer(logger.Info)
n, err := w.Write([]byte("some message\n"))
require.NoError(t, err)
want := `lvl=info msg="some message"` + "\n"
assert.Equal(t, 13, n)
assert.Equal(t, want, buf.String())
}
type fakeSpan struct {
Recording bool
ID byte
}
func (s *fakeSpan) IsRecording() bool {
return s.Recording
}
func (s *fakeSpan) SpanContext() trace.SpanContext {
return trace.NewSpanContext(trace.SpanContextConfig{
TraceID: [16]byte{1},
SpanID: [8]byte{s.ID},
Remote: false,
})
}