-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathctxlog_test.go
222 lines (202 loc) · 5.85 KB
/
ctxlog_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
package ctxlog
import (
"context"
"net/http"
"testing"
mocks "github.com/beatlabs/ctxlog/mocks/patron"
"github.com/beatlabs/patron/log"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestResolvesSameLogger(t *testing.T) {
t.Run("with some context values out of patron context", func(t *testing.T) {
// given
ctx := AddLoggerForRequest(testRequest())
// when
FromContext(ctx).Str("hello", "world")
loggerFromCtx := log.FromContext(ctx)
// then
castedLogger, castedOk := loggerFromCtx.(*CtxLogger)
assert.True(t, castedOk)
assert.Equal(t, FromContext(ctx).contextData, castedLogger.contextData)
})
t.Run("creates a new logger in context implicitly", func(t *testing.T) {
// given
ctx := AddLoggerForRequest(testRequest())
// when
loggerFromPatron := log.FromContext(ctx)
// then
assert.Same(t, FromContext(ctx), loggerFromPatron)
})
}
func TestCtxLogger(t *testing.T) {
tests := []struct {
Name string
ExpectedFields []map[string]interface{}
PrepareExpectedMessage func(logger *mocks.Logger)
ExpectedSubCalls int
LogCall func(context.Context)
AdditionalContextFields map[string]interface{}
}{
{
Name: "log error",
PrepareExpectedMessage: func(loggerMock *mocks.Logger) {
loggerMock.EXPECT().
Error(gomock.Any()).
DoAndReturn(func(input ...interface{}) {
require.Equal(t, 1, len(input))
assert.Equal(t, "error: 42", input[0])
}).Times(1)
},
ExpectedSubCalls: 1,
LogCall: func(ctx context.Context) {
FromContext(ctx).Errorf("error: %d", 42)
},
},
{
Name: "log info with sub",
ExpectedFields: []map[string]interface{}{
{
"foo": "bar",
},
},
PrepareExpectedMessage: func(loggerMock *mocks.Logger) {
loggerMock.EXPECT().
Info(gomock.Any()).
DoAndReturn(func(input ...interface{}) {
require.Equal(t, 1, len(input))
assert.Equal(t, "info: 42", input[0])
}).Times(1)
},
ExpectedSubCalls: 2,
LogCall: func(ctx context.Context) {
FromContext(ctx).Sub(map[string]interface{}{"foo": "bar"}).Infof("info: %d", 42)
},
},
{
Name: "log debug with Str",
PrepareExpectedMessage: func(loggerMock *mocks.Logger) {
loggerMock.EXPECT().
Debug(gomock.Any()).
DoAndReturn(func(input ...interface{}) {
require.Equal(t, 1, len(input))
assert.Equal(t, "error: 42", input[0])
}).Times(1)
},
ExpectedSubCalls: 1,
LogCall: func(ctx context.Context) {
FromContext(ctx).Str("hello", "world").Debugf("error: %d", 42)
},
AdditionalContextFields: map[string]interface{}{
"hello": "world",
},
},
{
Name: "log fatal with Int",
PrepareExpectedMessage: func(loggerMock *mocks.Logger) {
loggerMock.EXPECT().
Fatal(gomock.Any()).
DoAndReturn(func(input ...interface{}) {
require.Equal(t, 1, len(input))
assert.Equal(t, "fatal: 42", input[0])
}).Times(1)
},
ExpectedSubCalls: 1,
LogCall: func(ctx context.Context) {
FromContext(ctx).Int("my_key_int", 42).Fatalf("fatal: %d", 42)
},
AdditionalContextFields: map[string]interface{}{
"my_key_int": "42",
},
},
{
Name: "log panic with both Str and Int",
PrepareExpectedMessage: func(loggerMock *mocks.Logger) {
loggerMock.EXPECT().
Panic(gomock.Any()).
DoAndReturn(func(input ...interface{}) {
require.Equal(t, 1, len(input))
assert.Equal(t, "paniek: 42", input[0])
}).Times(1)
},
ExpectedSubCalls: 1,
LogCall: func(ctx context.Context) {
FromContext(ctx).Int("my_key_int", 42).Str("hello", "world").Panicf("paniek: %d", 42)
},
AdditionalContextFields: map[string]interface{}{
"my_key_int": "42",
"hello": "world",
},
},
{
Name: "log warn with SubCtx",
PrepareExpectedMessage: func(loggerMock *mocks.Logger) {
loggerMock.EXPECT().
Warn(gomock.Any()).
DoAndReturn(func(input ...interface{}) {
require.Equal(t, 1, len(input))
assert.Equal(t, "warn: 42", input[0])
}).Times(1)
},
ExpectedSubCalls: 1,
LogCall: func(ctx context.Context) {
FromContext(ctx).SubCtx(map[string]interface{}{
"val_1": 42,
"val_2": "hello world",
}).Warnf("warn: %d", 42)
},
AdditionalContextFields: map[string]interface{}{
"val_1": 42,
"val_2": "hello world",
},
},
}
for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
// given
fieldCounter := 0
ctx := AddLoggerForRequest(testRequest())
ctxLogger := FromContext(ctx)
loggerMock := mocks.NewLogger(gomock.NewController(t))
ctxLogger.logger = loggerMock
// then
test.PrepareExpectedMessage(loggerMock)
loggerMock.EXPECT().
Sub(gomock.Any()).
DoAndReturn(func(fields map[string]interface{}) log.Logger {
if fieldCounter+1 == test.ExpectedSubCalls {
ctxObj, ok := fields[CtxKey]
require.True(t, ok)
reqID := FromContext(ctx).RequestID()
ctxMap, ok := ctxObj.(map[string]interface{})
require.True(t, ok)
assert.Equal(t, reqID, ctxMap[RequestID])
if test.AdditionalContextFields != nil {
for k, v := range test.AdditionalContextFields {
assert.Equal(t, v, ctxMap[k])
}
}
return loggerMock
}
assert.Equal(t, test.ExpectedFields[fieldCounter], fields)
fieldCounter++
return loggerMock
}).Times(test.ExpectedSubCalls)
// when
test.LogCall(ctx)
})
}
}
func TestSubCtxChangesTheLoggerInContext(t *testing.T) {
ctx := AddLoggerForRequest(testRequest())
FromContext(ctx).SubCtx(map[string]interface{}{
"foo": "bar",
})
assert.Equal(t, "bar", FromContext(ctx).contextData["foo"])
}
func testRequest() *http.Request {
req, _ := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://foo", nil)
req.Header.Set(RequestID, "my-test-request-ID")
return req
}