-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglog_test.go
107 lines (90 loc) · 2.22 KB
/
glog_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
package glogadapter_test
import (
"context"
"regexp"
"strings"
"testing"
"github.com/elgopher/yala/adapter/glogadapter"
"github.com/elgopher/yala/adapter/internal/fake"
"github.com/elgopher/yala/logger"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAdapter_Log(t *testing.T) {
const message = "message"
t.Run("should log caller", func(t *testing.T) {
stderr := fake.UseFakeStderr(t)
defer stderr.Release()
adapter := glogadapter.Adapter{}
// when
adapter.Log(context.Background(), logger.Entry{
Level: logger.ErrorLevel,
Message: message,
})
// then
msg := unmarshalLine(t, stderr.String(t))
const expectedPrefix = "glog_test.go:"
assert.Truef(t,
strings.HasPrefix(msg.caller, expectedPrefix),
"caller %s has no prefix %s", msg.caller, expectedPrefix)
})
t.Run("should log message", func(t *testing.T) {
stderr := fake.UseFakeStderr(t)
defer stderr.Release()
adapter := glogadapter.Adapter{}
// when
adapter.Log(context.Background(), logger.Entry{
Level: logger.ErrorLevel,
Message: message,
})
// then
msg := unmarshalLine(t, stderr.String(t))
assert.Equal(t, "E", msg.level)
assert.Equal(t, message, msg.message)
})
t.Run("should log message with field", func(t *testing.T) {
stderr := fake.UseFakeStderr(t)
defer stderr.Release()
adapter := glogadapter.Adapter{}
// when
entry := logger.Entry{
Level: logger.ErrorLevel,
Message: message,
}.With(logger.Field{
Key: "k",
Value: "v",
})
adapter.Log(context.Background(), entry)
// then
msg := unmarshalLine(t, stderr.String(t))
assert.Equal(t, "k=v", msg.fields)
assert.Equal(t, message, msg.message)
})
}
func unmarshalLine(t *testing.T, line string) glogMessage {
t.Helper()
r := regexp.MustCompile(`\s+`)
parts := r.Split(line, 6)
require.Len(t, parts, 6)
level := parts[0]
level = level[:1]
caller := parts[3]
caller = caller[:len(caller)-1]
message := parts[4]
fields := parts[5]
if len(fields) > 0 {
fields = fields[:len(fields)-1]
}
return glogMessage{
caller: caller,
level: level,
message: message,
fields: fields,
}
}
type glogMessage struct {
caller string
level string
message string
fields string
}