-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_test.go
486 lines (425 loc) · 12.5 KB
/
log_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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
package log_test
import (
"testing"
"github.com/flowpl/log"
)
const FORMATTED_MESSAGE = "message returned from dummyFormatter"
type DummyFormatOutput struct {
level string
message string
tags map[string]string
dateFormat string
outputMessage string
}
func (dfo *DummyFormatOutput) createDummyFormatter() func(string, string, map[string]string, string) string{
return func(level string, message string, tags map[string]string, dateFormat string) string {
dfo.level = level
dfo.message = message
dfo.tags = tags
dfo.dateFormat = dateFormat
return FORMATTED_MESSAGE
}
}
func (dfo *DummyFormatOutput) createDummyOutput() func(string) {
return func (message string) {
dfo.outputMessage = message
}
}
func TestNewLoggerCreatesANewLogThatUsesGivenConfig(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_DEBUG,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
map[string]string{"tag1":"value1"},
}
var result log.Logger = log.NewLogger(config)
result.Debug("message", map[string]string{})
if dfo.level != log.LEVEL_DEBUG {
t.Errorf("expected level to be \"%s\", actual: \"%s\"", log.LEVEL_DEBUG, dfo.level)
}
if dfo.message != "message" {
t.Errorf("expected message to be \"%s\", actual: \"%s\"", "message", dfo.message)
}
if dfo.tags["tag1"] != "value1" {
t.Errorf("expected tag \"tag1\" to be \"value1\", actual: \"%s\"", dfo.tags["tag1"])
}
if dfo.tags["function"] != "main" {
t.Errorf("expected tag \"function\" to be \"main\", actual: \"%s\"", dfo.tags["function"])
}
if dfo.tags["program"] != "log_test" {
t.Errorf("expected tag \"program\" to be \"log_test\", actual: \"%s\"", dfo.tags["program"])
}
if len(dfo.tags) != 3 {
t.Errorf("expected exactly %d tags, actual: %d", 3, len(dfo.tags))
}
if dfo.dateFormat != "2006" {
t.Errorf("expected dateFormat to be \"%s\", actual: \"%s\"", "2006", dfo.dateFormat)
}
if dfo.outputMessage != FORMATTED_MESSAGE {
t.Errorf("expected outputMessage to be \"%s\", actual: \"%s\"", FORMATTED_MESSAGE, dfo.outputMessage)
}
}
func TestNewLoggerShouldAcceptNilAsTagsConfig(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_DEBUG,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
result := log.NewLogger(config)
result.Debug("message", map[string]string{})
if len(dfo.tags) != 2 {
t.Errorf("expected exactly 2 tags, actual: %d", len(dfo.tags))
}
}
func TestLog_InfoShouldOutputMessagesIfLevelIsDebug(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_DEBUG,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
result := log.NewLogger(config)
result.Info("message", map[string]string{})
if dfo.message != "message" {
t.Errorf("expected message to be \"%s\", actual: \"%s\"", "message", dfo.message)
}
if dfo.outputMessage != FORMATTED_MESSAGE {
t.Errorf("expected outputMessage to be \"%s\", actual \"%s\"", FORMATTED_MESSAGE, dfo.outputMessage)
}
}
func TestLog_InfoShouldOutputMessagesIfLevelIsInfo(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
result := log.NewLogger(config)
result.Info("message", map[string]string{})
if dfo.message != "message" {
t.Errorf("expected message to be \"%s\", actual: \"%s\"", "message", dfo.message)
}
if dfo.outputMessage != FORMATTED_MESSAGE {
t.Errorf("expected outputMessage to be \"%s\", actual \"%s\"", FORMATTED_MESSAGE, dfo.outputMessage)
}
}
func TestLog_InfoShouldReturnInvalidContext(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
result := log.NewLogger(config)
err := result.Info("message", "")
if _, ok := err.(*log.InvalidContext); !ok {
t.Error("expected InvalidContext error")
}
}
func TestLog_DebugShouldOutputMessagesIfLevelIsDebug(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_DEBUG,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
result := log.NewLogger(config)
result.Debug("message", map[string]string{})
if dfo.message != "message" {
t.Errorf("expected message to be \"%s\", actual: \"%s\"", "message", dfo.message)
}
if dfo.outputMessage != FORMATTED_MESSAGE {
t.Errorf("expected outputMessage to be \"%s\", actual \"%s\"", FORMATTED_MESSAGE, dfo.outputMessage)
}
}
func TestLog_DebugShouldNotOutputMessagesIfLevelIsInfo(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
result := log.NewLogger(config)
result.Debug("message", map[string]string{})
if dfo.message != "" {
t.Errorf("expected message to be \"%s\", actual: \"%s\"", "message", dfo.message)
}
if dfo.outputMessage != "" {
t.Errorf("expected outputMessage to be [empty string], actual \"%s\"", dfo.outputMessage)
}
}
func TestLog_DebugShouldReturnInvalidContext(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_DEBUG,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
result := log.NewLogger(config)
err := result.Debug("message", "")
if _, ok := err.(*log.InvalidContext); !ok {
t.Error("expected InvalidContext error")
}
}
func TestLog_ChildLoggerShouldCreateANewLoggerInstance(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", map[string]string{})
result.Info("message", map[string]string{})
if parentLogger == result {
t.Error("expected logger and childLogger to be different instances, but they are the same")
}
}
func TestLog_ChildLoggerShouldMergeItsContextWithParentLoggersTags(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", map[string]string{"child_tag":"value"})
result.Info("message", map[string]string{})
if len(dfo.tags) != 3 {
t.Errorf("expected exactly 3 tags, actual: \"%d\"", len(dfo.tags))
}
if dfo.tags["child_tag"] != "value" {
t.Errorf("expected tag child_tag to be \"%s\", actual: \"%s\"", "message", dfo.tags["child_tag"])
}
}
func TestLog_ChildLoggerShouldSetFunctionNameOnTheChildLoggerButLeaveTheParentTagsUnchanged(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", map[string]string{})
result.Info("child", map[string]string{})
if dfo.tags["function"] != "child_test" {
t.Errorf("expected child logger function tag to be \"child_test\", actual: \"%s\"", dfo.tags["function"])
}
parentLogger.Info("parent", map[string]string{})
if dfo.tags["function"] != "main" {
t.Errorf("expected parent logger function tag to be \"main\", actual: \"%s\"", dfo.tags["function"])
}
}
func TestLog_ChildLoggerShouldAllowNilAsContext(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", nil)
result.Info("message", map[string]string{})
if len(dfo.tags) != 2 {
t.Errorf("expected exactls 2 tags, actual: \"%d\"", len(dfo.tags))
}
}
type arbitraryStruct struct {
ExportedStructTag string
structTag string
}
func TestLog_ChildLoggerShouldAcceptAnArbitraryStructAsContextAndMergeExportedFieldsIntoTags(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", arbitraryStruct{"exportedValue", "value"})
result.Info("message", map[string]string{})
if len(dfo.tags) != 3 {
t.Errorf("expected exactly 3 tags, actual: \"%d\"", len(dfo.tags))
}
if dfo.tags["ExportedStructTag"] != "exportedValue" {
t.Errorf("expected tag ExportedStructTag to be \"exportedValue\", actual: \"%s\"", dfo.tags["ExportedStructTag"])
}
}
func TestLog_ChildLoggerShouldAcceptAPtrToArbitraryStructAsContextAndMergeExportedFieldsIntoTags(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", &arbitraryStruct{"exportedValue", "value"})
result.Info("message", map[string]string{})
if len(dfo.tags) != 3 {
t.Errorf("expected exactly 3 tags, actual: \"%d\"", len(dfo.tags))
}
if dfo.tags["ExportedStructTag"] != "exportedValue" {
t.Errorf("expected tag ExportedStructTag to be \"exportedValue\", actual: \"%s\"", dfo.tags["ExportedStructTag"])
}
}
type arbitraryStringer struct {
ExportedStructTag string
structTag string
}
func (as arbitraryStringer) String() string {
return "result from stringer"
}
func TestLog_ChildLoggerShouldUseTheStringerInterfaceOfContextIfPresent(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", arbitraryStringer{"exportedValue", "value"})
result.Info("message", map[string]string{})
if len(dfo.tags) != 3 {
t.Errorf("expected exactly 3 tags, actual: \"%s\"", len(dfo.tags))
}
if dfo.tags["context"] != "result from stringer" {
t.Errorf("expected tag context to be \"%s\", actual: \"%s\"", "result from stringer", dfo.tags["context"])
}
}
type arbitraryError struct {
ExportedStructTag string
structTag string
}
func (as arbitraryError) Error() string {
return "result from error"
}
func TestLog_ChildLoggerShouldUseTheErrorInterfaceOfContextIfPresent(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", arbitraryError{"exportedValue", "value"})
result.Info("message", map[string]string{})
if len(dfo.tags) != 3 {
t.Errorf("expected exactly 3 tags, actual: \"%s\"", len(dfo.tags))
}
if dfo.tags["error"] != "result from error" {
t.Errorf("expected tag context to be \"%s\", actual: \"%s\"", "result from error", dfo.tags["error"])
}
}
type arbitraryErrorStringer struct {
ExportedStructTag string
structTag string
}
func (as arbitraryErrorStringer) Error() string {
return "result from error"
}
func (as arbitraryErrorStringer) String() string {
return "result from stringer"
}
func TestLog_ChildLoggerShouldPreferErrorOverStringer(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
result, _ := parentLogger.ChildLogger("child_test", arbitraryErrorStringer{"exportedValue", "value"})
result.Info("message", map[string]string{})
if len(dfo.tags) != 3 {
t.Errorf("expected exactly 3 tags, actual: \"%s\"", len(dfo.tags))
}
if dfo.tags["error"] != "result from error" {
t.Errorf("expected tag context to be \"%s\", actual: \"%s\"", "result from error", dfo.tags["error"])
}
}
func TestLog_ChildLoggerShouldReturnInvalidContextOnInvalidContext(t *testing.T) {
dfo := new(DummyFormatOutput)
config := &log.Config{
log.LEVEL_INFO,
dfo.createDummyFormatter(),
dfo.createDummyOutput(),
"log_test",
"main",
"2006",
nil,
}
parentLogger := log.NewLogger(config)
_, err := parentLogger.ChildLogger("child_test", "")
if _, ok := err.(*log.InvalidContext); !ok {
t.Error("expected InvalidContext error")
}
}