-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger_core_fallback_test.go
273 lines (208 loc) · 7.65 KB
/
logger_core_fallback_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
package log
import (
"bytes"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/echocat/slf4g/fields"
"github.com/echocat/slf4g/level"
"github.com/echocat/slf4g/internal/test/assert"
)
func Test_fallbackCoreLogger_Log(t *testing.T) {
givenError := errors.New("expected")
instance, buf := newFallbackCoreLogger("foo")
t2 := time.Now()
t1 := t2.Add(-2 * time.Minute)
// WARNING! Do not move these lines, because the test relies on it.
// I know this could be better... ;-)
instance.Log(instance.NewEvent(level.Trace, nil).
With("a", 11).
With("b", 12).
With("message", "hello").
With("timestamp", t1), 0)
instance.Log(instance.NewEvent(level.Info, nil).
With("a", 11).
With("b", 12).
With("timestamp", t1).
With("logger", fallbackRootLoggerName), 0)
instance.Log(instance.NewEvent(level.Error, nil).
With("a", 21).
With("c", 23).
With("d", fields.Exclude).
With("message", " hello ").
WithError(givenError).
With("timestamp", t2), 0)
assert.ToBeEqual(t, fmt.Sprintf(strings.TrimLeft(`
I%s %d logger_core_fallback_test.go:32] a=11 b=12
E%s %d logger_core_fallback_test.go:37] hello a=21 c=23 error="expected" logger="foo"
`, "\n"),
t1.Format(simpleTimeLayout), pid,
t2.Format(simpleTimeLayout), pid,
), buf.String())
}
func Test_fallbackCoreLogger_formatLocation(t *testing.T) {
instance, _ := newFallbackCoreLogger("foo")
// WARNING! Do not move these lines, because the test relies on it.
// I know this could be better... ;-)
assert.ToBeEqual(t, "logger_core_fallback_test.go:59", instance.formatLocation(0))
assert.ToBeEqual(t, "???:?", instance.formatLocation(1000))
}
func Test_fallbackCoreLogger_Log_withoutTimestamp(t *testing.T) {
instance, buf := newFallbackCoreLogger("foo")
instance.Log(instance.NewEvent(level.Info, nil), 0)
assert.ToBeMatching(t, `^I\d{2}\d{2} \d{2}:\d{2}:\d{2}\.\d{6} \d+ logger_core_fallback_test.go:\d+] logger="foo"`, buf.String())
}
func Test_fallbackCoreLogger_Log_withLazyValue(t *testing.T) {
instance, buf := newFallbackCoreLogger("foo")
instance.Log(instance.NewEvent(level.Info, nil).
With("foo", lazyMock(666)), 0)
assert.ToBeMatching(t, `^I.+logger_core_fallback_test.go:\d+] foo=666 logger="foo"`, buf.String())
}
func Test_fallbackCoreLogger_Log_withFilteredValue_respected(t *testing.T) {
instance, buf := newFallbackCoreLogger("foo")
instance.Log(instance.NewEvent(level.Info, nil).
With("foo", fields.RequireMaximalLevel(level.Info, 666)), 0)
assert.ToBeMatching(t, `^I.+logger_core_fallback_test.go:\d+] foo=666 logger="foo"`, buf.String())
}
func Test_fallbackCoreLogger_Log_withFilteredValue_ignored(t *testing.T) {
instance, buf := newFallbackCoreLogger("foo")
instance.Log(instance.NewEvent(level.Info, nil).
With("foo", fields.RequireMaximalLevel(level.Debug, 666)), 0)
assert.ToBeMatching(t, `^I.+logger_core_fallback_test.go:\d+] logger="foo"`, buf.String())
}
func Test_fallbackCoreLogger_Log_brokenCallDepth(t *testing.T) {
instance, buf := newFallbackCoreLogger("foo")
instance.Log(instance.NewEvent(level.Info, nil), 10000)
assert.ToBeMatching(t, `^I.+ \d+ \?\?\?:\?] logger="foo"`, buf.String())
}
func Test_fallbackCoreLogger_Log_withErrorWhileMarshalling(t *testing.T) {
instance, buf := newFallbackCoreLogger("foo")
instance.Log(instance.NewEvent(level.Info, nil).
With("foo", failingJsonMarshalling("expected")), 0)
assert.ToBeMatching(t, `^ERR!! Cannot format event.+: expected`, buf.String())
}
func Test_fallbackCoreLogger_Log_levels(t *testing.T) {
cases := []struct {
expectedC string
level level.Level
}{
{"T", level.Trace},
{"D", level.Debug},
{"I", level.Info},
{"W", level.Warn},
{"E", level.Error},
{"F", level.Fatal},
{"?", level.Level(6666)},
}
for _, c := range cases {
t.Run(c.expectedC, func(t *testing.T) {
instance, buf := newFallbackCoreLogger("foo")
instance.level = 1
instance.Log(instance.NewEvent(c.level, nil), 0)
assert.ToBeMatching(t, `^`+c.expectedC+`\d{2}\d{2} \d{2}:\d{2}:\d{2}\.\d{6} \d+ logger_core_fallback_test.go:\d+] logger="foo"`, buf.String())
})
}
}
func Test_fallbackCoreLogger_GetName(t *testing.T) {
instance := &fallbackCoreLogger{name: "foo"}
actual := instance.GetName()
assert.ToBeEqual(t, "foo", actual)
}
func Test_fallbackCoreLogger_GetProvider(t *testing.T) {
givenProvider := &fallbackProvider{}
instance := &fallbackCoreLogger{fallbackProvider: givenProvider}
actual := instance.GetProvider()
assert.ToBeSame(t, givenProvider, actual)
}
func Test_fallbackCoreLogger_GetLevel(t *testing.T) {
instance, _ := newFallbackCoreLogger("foo")
assert.ToBeEqual(t, level.Info, instance.GetLevel())
instance.fallbackProvider.level = level.Warn
assert.ToBeEqual(t, level.Warn, instance.GetLevel())
for _, l := range instance.GetAllLevels() {
instance.level = l
assert.ToBeEqual(t, l, instance.GetLevel())
}
instance.level = 0
assert.ToBeEqual(t, level.Warn, instance.GetLevel())
}
func Test_fallbackCoreLogger_SetLevel(t *testing.T) {
instance, _ := newFallbackCoreLogger("foo")
assert.ToBeEqual(t, level.Level(0), instance.level)
for _, l := range instance.GetAllLevels() {
instance.SetLevel(l)
assert.ToBeEqual(t, l, instance.level)
}
instance.SetLevel(0)
assert.ToBeEqual(t, level.Level(0), instance.level)
}
func Test_fallbackCoreLogger_NewEvent(t *testing.T) {
instance, _ := newFallbackCoreLogger("foo")
assert.ToBeEqual(t, &fallbackEvent{
provider: instance,
fields: fields.Empty(),
level: level.Fatal,
}, instance.NewEvent(level.Fatal, nil))
assert.ToBeEqual(t, &fallbackEvent{
provider: instance,
fields: fields.WithAll(map[string]interface{}{"foo": "bar"}),
level: level.Fatal,
}, instance.NewEvent(level.Fatal, map[string]interface{}{"foo": "bar"}))
}
func Test_fallbackCoreLogger_NewEventWithFields(t *testing.T) {
instance, _ := newFallbackCoreLogger("foo")
assert.ToBeEqual(t, &fallbackEvent{
provider: instance,
fields: fields.Empty(),
level: level.Fatal,
}, instance.NewEventWithFields(level.Fatal, nil))
assert.ToBeEqual(t, &fallbackEvent{
provider: instance,
fields: fields.With("foo", "bar"),
level: level.Fatal,
}, instance.NewEventWithFields(level.Fatal, fields.With("foo", "bar")))
}
func Test_fallbackCoreLogger_NewEventWithFields_panicsOnErrors(t *testing.T) {
instance, _ := newFallbackCoreLogger("foo")
assert.Execution(t, func() {
instance.NewEventWithFields(level.Fatal, fields.ForEachFunc(func(func(string, interface{}) error) error {
return errors.New("expected")
}))
}).WillPanicWith("^expected$")
}
func Test_fallbackCoreLogger_Accepts(t *testing.T) {
instance, _ := newFallbackCoreLogger("foo")
assert.ToBeEqual(t, false, instance.Accepts(nil))
assert.ToBeEqual(t, true, instance.Accepts(&fallbackEvent{}))
}
func Test_IsFallbackLogger(t *testing.T) {
givenMockedProvider := newMockProvider("foo")
givenMockedProvider.rootProvider = func() Logger {
return newMockLogger("root")
}
previous := SetProvider(nil)
defer SetProvider(previous)
assert.ToBeEqual(t, true, IsFallbackLogger(GetRootLogger()))
assert.ToBeEqual(t, []Provider{}, GetAllProviders())
SetProvider(givenMockedProvider)
assert.ToBeEqual(t, false, IsFallbackLogger(GetRootLogger()))
SetProvider(nil)
assert.ToBeEqual(t, true, IsFallbackLogger(GetRootLogger()))
}
func newFallbackCoreLogger(name string) (*fallbackCoreLogger, *bytes.Buffer) {
buf := new(bytes.Buffer)
provider := &fallbackProvider{
out: buf,
}
provider.cache = NewLoggerCache(provider.rootFactory, provider.factory)
return &fallbackCoreLogger{
fallbackProvider: provider,
name: name,
}, buf
}
type failingJsonMarshalling string
func (instance failingJsonMarshalling) MarshalJSON() ([]byte, error) {
return nil, stringError(instance)
}