-
Notifications
You must be signed in to change notification settings - Fork 4
/
stdlogger_test.go
158 lines (148 loc) · 5.23 KB
/
stdlogger_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
package golog_test
import (
"context"
"testing"
. "github.com/damianopetrungaro/golog"
)
func TestStdLogger(t *testing.T) {
ctx := context.Background()
tests := map[string]struct {
w Writer
log func(StdLogger, context.Context, Message)
wantEntry Entry
}{
"debug logger must write an debug entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Debug(ctx, msg) },
wantEntry: debugEntry,
},
"debug logger must write an info entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Info(ctx, msg) },
wantEntry: infoEntry,
},
"debug logger must write an warn entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Warn(ctx, msg) },
wantEntry: warnEntry,
},
"debug logger must write an error entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Error(ctx, msg) },
wantEntry: errorEntry,
},
"debug logger must write an fatal entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Fatal(ctx, msg) },
wantEntry: fatalEntry,
},
"info logger must write an debug entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Debug(ctx, msg) },
wantEntry: debugEntry,
},
"info logger must write an info entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Info(ctx, msg) },
wantEntry: infoEntry,
},
"info logger must write an warn entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Warn(ctx, msg) },
wantEntry: warnEntry,
},
"info logger must write an error entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Error(ctx, msg) },
wantEntry: errorEntry,
},
"info logger must write an fatal entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Fatal(ctx, msg) },
wantEntry: fatalEntry,
},
"warn logger must write an debug entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Debug(ctx, msg) },
wantEntry: debugEntry,
},
"warn logger must write an info entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Info(ctx, msg) },
wantEntry: infoEntry,
},
"warn logger must write an warn entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Warn(ctx, msg) },
wantEntry: warnEntry,
},
"warn logger must write an error entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Error(ctx, msg) },
wantEntry: errorEntry,
},
"warn logger must write an fatal entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Fatal(ctx, msg) },
wantEntry: fatalEntry,
},
"error logger must write an debug entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Debug(ctx, msg) },
wantEntry: debugEntry,
},
"error logger must write an info entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Info(ctx, msg) },
wantEntry: infoEntry,
},
"error logger must write an warn entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Warn(ctx, msg) },
wantEntry: warnEntry,
},
"error logger must write an error entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Error(ctx, msg) },
wantEntry: errorEntry,
},
"error logger must write an fatal entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Fatal(ctx, msg) },
wantEntry: fatalEntry,
},
"fatal logger must write an debug entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Debug(ctx, msg) },
wantEntry: debugEntry,
},
"fatal logger must write an info entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Info(ctx, msg) },
wantEntry: infoEntry,
},
"fatal logger must write an warn entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Warn(ctx, msg) },
wantEntry: warnEntry,
},
"fatal logger must write an error entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Error(ctx, msg) },
wantEntry: errorEntry,
},
"fatal logger must write an fatal entry": {
log: func(l StdLogger, ctx context.Context, msg Message) { l.Fatal(ctx, msg) },
wantEntry: fatalEntry,
},
}
for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
w := &FakeWriter{}
defer func() {
EntryMatcher(t, test.wantEntry, w.Entry)
}()
defer func() {
// nolint:staticcheck
if r := recover(); r != nil {
}
}()
test.log(New(w), ctx, msg)
})
}
}
func TestStdLogger_With(t *testing.T) {
var l Logger = New(&FakeWriter{}, OptionFunc(func(l StdLogger) StdLogger {
return l.WithDecorator(DecoratorFunc(func(e Entry) Entry { return e }))
}))
flds := Fields{String("a", "A"), String("b", "B")}
l = l.With(flds...)
if len(l.(StdLogger).Decorators) != 1 {
t.Fatal("could not match decorators")
}
FieldMatcher(t, flds, l.(StdLogger).Fields)
otherFlds := Fields{String("c", "C"), String("d", "D")}
l = l.With(otherFlds...)
if len(l.(StdLogger).Decorators) != 1 {
t.Fatal("could not match decorators")
}
FieldMatcher(t, append(flds, otherFlds...), l.(StdLogger).Fields)
}