-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger_writer_test.go
173 lines (141 loc) · 5.68 KB
/
logger_writer_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
package log
import (
"bytes"
"strings"
"testing"
"github.com/echocat/slf4g/internal/test/assert"
"github.com/echocat/slf4g/level"
)
func Test_LoggingWriter_Write(t *testing.T) {
givenLogger := newMockCoreLogger("foo")
givenLogger.initLoggedEvents()
givenExtractor := level.LineExtractorFunc(func(in []byte) (level.Level, error) {
if strings.HasPrefix(string(in), "I") {
return level.Info, nil
}
if strings.HasPrefix(string(in), "E") {
return level.Error, nil
}
panic("not expected")
})
messageKey := givenLogger.GetProvider().GetFieldKeysSpec().GetMessage()
instance := &LoggingWriter{
Logger: givenLogger,
LevelExtractor: givenExtractor,
SkipFrames: 666,
}
actual1Written, actual1Err := instance.Write([]byte("I hello world!"))
assert.ToBeNil(t, actual1Err)
assert.ToBeEqual(t, 14, actual1Written)
actual2Written, actual2Err := instance.Write([]byte("E hello world!"))
assert.ToBeNil(t, actual2Err)
assert.ToBeEqual(t, 14, actual2Written)
assert.ToBeEqual(t, 2, len(*givenLogger.loggedEvents))
assert.ToBeEqual(t, level.Info, givenLogger.loggedEvent(0).GetLevel())
actualMessage0, _ := givenLogger.loggedEvent(0).Get(messageKey)
assert.ToBeEqual(t, "I hello world!", actualMessage0)
assert.ToBeEqual(t, level.Error, givenLogger.loggedEvent(1).GetLevel())
actualMessage1, _ := givenLogger.loggedEvent(1).Get(messageKey)
assert.ToBeEqual(t, "E hello world!", actualMessage1)
}
func Test_LoggingWriter_Write_withoutLevelExtractor(t *testing.T) {
givenLogger := newMockCoreLogger("foo")
givenLogger.initLoggedEvents()
messageKey := givenLogger.GetProvider().GetFieldKeysSpec().GetMessage()
instance := &LoggingWriter{
Logger: givenLogger,
SkipFrames: 666,
}
actual1Written, actual1Err := instance.Write([]byte("W hello world!"))
assert.ToBeNil(t, actual1Err)
assert.ToBeEqual(t, 14, actual1Written)
actual2Written, actual2Err := instance.Write([]byte("E hello world!"))
assert.ToBeNil(t, actual2Err)
assert.ToBeEqual(t, 14, actual2Written)
assert.ToBeEqual(t, 2, len(*givenLogger.loggedEvents))
assert.ToBeEqual(t, level.Info, givenLogger.loggedEvent(0).GetLevel())
actualMessage0, _ := givenLogger.loggedEvent(0).Get(messageKey)
assert.ToBeEqual(t, "W hello world!", actualMessage0)
assert.ToBeEqual(t, level.Info, givenLogger.loggedEvent(1).GetLevel())
actualMessage1, _ := givenLogger.loggedEvent(1).Get(messageKey)
assert.ToBeEqual(t, "E hello world!", actualMessage1)
}
func Test_LoggingWriter_Write_lineExtractorErrors(t *testing.T) {
givenLogger := newMockCoreLogger("foo")
givenLogger.initLoggedEvents()
givenExtractor := level.LineExtractorFunc(func(in []byte) (level.Level, error) {
return 0, stringError(in)
})
instance := &LoggingWriter{Logger: givenLogger, LevelExtractor: givenExtractor}
actual1Written, actual1Err := instance.Write([]byte("foo"))
assert.ToBeEqual(t, stringError("foo"), actual1Err)
assert.ToBeEqual(t, 0, actual1Written)
actual2Written, actual2Err := instance.Write([]byte("bar"))
assert.ToBeEqual(t, stringError("bar"), actual2Err)
assert.ToBeEqual(t, 0, actual2Written)
assert.ToBeEqual(t, 0, len(*givenLogger.loggedEvents))
}
func Test_LoggingWriter_Write_withoutLogger(t *testing.T) {
instance := &LoggingWriter{}
actual1Written, actual1Err := instance.Write([]byte("foo"))
assert.ToBeNil(t, actual1Err)
assert.ToBeEqual(t, 3, actual1Written)
actual2Written, actual2Err := instance.Write([]byte("foobar"))
assert.ToBeNil(t, actual2Err)
assert.ToBeEqual(t, 6, actual2Written)
}
func Test_LoggingWriter_Write_withInterceptor(t *testing.T) {
givenLogger := newMockCoreLogger("foo")
givenLogger.initLoggedEvents()
givenInterceptor := func(in []byte, lvl level.Level) ([]byte, level.Level, error) {
assert.ToBeEqual(t, level.Warn, lvl)
if bytes.HasPrefix(in, []byte("I ")) {
return in[2:], level.Info, nil
}
if bytes.HasPrefix(in, []byte("E ")) {
return in[2:], level.Error, nil
}
panic("not expected")
}
messageKey := givenLogger.GetProvider().GetFieldKeysSpec().GetMessage()
instance := &LoggingWriter{
Logger: givenLogger,
LevelExtractor: level.FixedLevelExtractor(level.Warn),
Interceptor: givenInterceptor,
SkipFrames: 666,
}
actual1Written, actual1Err := instance.Write([]byte("I hello world!"))
assert.ToBeNil(t, actual1Err)
assert.ToBeEqual(t, 12, actual1Written)
actual2Written, actual2Err := instance.Write([]byte("E hello world!"))
assert.ToBeNil(t, actual2Err)
assert.ToBeEqual(t, 12, actual2Written)
assert.ToBeEqual(t, 2, len(*givenLogger.loggedEvents))
assert.ToBeEqual(t, level.Info, givenLogger.loggedEvent(0).GetLevel())
actualMessage0, _ := givenLogger.loggedEvent(0).Get(messageKey)
assert.ToBeEqual(t, "hello world!", actualMessage0)
assert.ToBeEqual(t, level.Error, givenLogger.loggedEvent(1).GetLevel())
actualMessage1, _ := givenLogger.loggedEvent(1).Get(messageKey)
assert.ToBeEqual(t, "hello world!", actualMessage1)
}
func Test_LoggingWriter_Write_interceptorErrors(t *testing.T) {
givenLogger := newMockCoreLogger("foo")
givenLogger.initLoggedEvents()
givenInterceptor := func(in []byte, lvl level.Level) ([]byte, level.Level, error) {
assert.ToBeEqual(t, level.Warn, lvl)
return nil, 0, stringError(in)
}
instance := &LoggingWriter{
Logger: givenLogger,
LevelExtractor: level.FixedLevelExtractor(level.Warn),
Interceptor: givenInterceptor,
SkipFrames: 666,
}
actual1Written, actual1Err := instance.Write([]byte("foo"))
assert.ToBeEqual(t, stringError("foo"), actual1Err)
assert.ToBeEqual(t, 0, actual1Written)
actual2Written, actual2Err := instance.Write([]byte("bar"))
assert.ToBeEqual(t, stringError("bar"), actual2Err)
assert.ToBeEqual(t, 0, actual2Written)
assert.ToBeEqual(t, 0, len(*givenLogger.loggedEvents))
}