generated from logur/adapter-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger_test.go
70 lines (56 loc) · 1.4 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
package zap
import (
"bytes"
"encoding/json"
"strings"
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"logur.dev/logur"
"logur.dev/logur/conformance"
)
// nolint: gochecknoglobals
var levelMap = map[logur.Level]zapcore.Level{
logur.Trace: zap.DebugLevel,
logur.Debug: zap.DebugLevel,
logur.Info: zap.InfoLevel,
logur.Warn: zap.WarnLevel,
logur.Error: zap.ErrorLevel,
}
func TestLogger(t *testing.T) {
suite := conformance.TestSuite{
NoTraceLevel: true,
LoggerFactory: func(level logur.Level) (logur.Logger, conformance.TestLogger) {
var buf bytes.Buffer
logger := zap.New(
zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
zapcore.AddSync(&buf),
levelMap[level],
),
)
return New(logger), conformance.TestLoggerFunc(func() []logur.LogEvent {
lines := strings.Split(strings.TrimSuffix(buf.String(), "\n"), "\n")
events := make([]logur.LogEvent, len(lines))
for key, line := range lines {
log := strings.SplitN(line, "\t", 4)
level, _ := logur.ParseLevel(strings.ToLower(log[1]))
var fields map[string]interface{}
if len(log) > 3 {
err := json.Unmarshal([]byte(log[3]), &fields)
if err != nil {
panic(err)
}
}
events[key] = logur.LogEvent{
Line: log[2],
Level: level,
Fields: fields,
}
}
return events
})
},
}
suite.Run(t)
}