generated from logur/adapter-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger_test.go
58 lines (44 loc) · 1.23 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
package hclog
import (
"bytes"
"regexp"
"strings"
"testing"
"github.com/hashicorp/go-hclog"
"logur.dev/logur"
"logur.dev/logur/conformance"
)
// nolint: gochecknoglobals
var logLineRegex = regexp.MustCompile(`.* \[(.*)\] {1,2}(.*): (.*)`)
func TestLogger(t *testing.T) {
suite := conformance.TestSuite{
LoggerFactory: func(level logur.Level) (logur.Logger, conformance.TestLogger) {
var buf bytes.Buffer
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Level(level + 1),
Output: &buf,
})
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 {
match := logLineRegex.FindStringSubmatch(line)
level, _ := logur.ParseLevel(strings.ToLower(match[1]))
rawFields := strings.Fields(match[3])
fields := make(map[string]interface{})
for _, rawField := range rawFields {
field := strings.SplitN(rawField, "=", 2)
fields[field[0]] = field[1]
}
events[key] = logur.LogEvent{
Line: match[2],
Level: level,
Fields: fields,
}
}
return events
})
},
}
suite.Run(t)
}