-
Notifications
You must be signed in to change notification settings - Fork 28
/
logger_test.go
448 lines (351 loc) · 12.1 KB
/
logger_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
package lager_test
import (
"errors"
"fmt"
"net/http"
"runtime"
"strconv"
"time"
"code.cloudfoundry.org/lager/v3"
"code.cloudfoundry.org/lager/v3/lagertest"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("Logger", func() {
var logger lager.Logger
var testSink *lagertest.TestSink
var component = "my-component"
var action = "my-action"
var logData = lager.Data{
"foo": "bar",
"a-number": 7,
}
var anotherLogData = lager.Data{
"baz": "quux",
"b-number": 43,
}
BeforeEach(func() {
logger = lager.NewLogger(component)
testSink = lagertest.NewTestSink()
logger.RegisterSink(testSink)
})
var TestCommonLogFeatures = func(level lager.LogLevel) {
var log lager.LogFormat
BeforeEach(func() {
log = testSink.Logs()[0]
})
It("writes a log to the sink", func() {
Expect(testSink.Logs()).To(HaveLen(1))
})
It("records the source component", func() {
Expect(log.Source).To(Equal(component))
})
It("outputs a properly-formatted message", func() {
Expect(log.Message).To(Equal(fmt.Sprintf("%s.%s", component, action)))
})
It("has a timestamp", func() {
expectedTime := float64(time.Now().UnixNano()) / 1e9
parsedTimestamp, err := strconv.ParseFloat(log.Timestamp, 64)
Expect(err).NotTo(HaveOccurred())
Expect(parsedTimestamp).To(BeNumerically("~", expectedTime, 1.0))
})
It("sets the proper output level", func() {
Expect(log.LogLevel).To(Equal(level))
})
}
var TestLogData = func() {
var log lager.LogFormat
BeforeEach(func() {
log = testSink.Logs()[0]
})
It("data contains custom user data", func() {
Expect(log.Data["foo"]).To(Equal("bar"))
Expect(log.Data["a-number"]).To(BeNumerically("==", 7))
Expect(log.Data["baz"]).To(Equal("quux"))
Expect(log.Data["b-number"]).To(BeNumerically("==", 43))
})
}
Describe("Session", func() {
var session lager.Logger
BeforeEach(func() {
session = logger.Session("sub-action")
})
Describe("the returned logger", func() {
JustBeforeEach(func() {
session.Debug("some-debug-action", lager.Data{"level": "debug"})
session.Info("some-info-action", lager.Data{"level": "info"})
session.Error("some-error-action", errors.New("oh no!"), lager.Data{"level": "error"})
defer func() {
recover() //nolint:errcheck
}()
session.Fatal("some-fatal-action", errors.New("oh no!"), lager.Data{"level": "fatal"})
})
It("logs with a shared session id in the data", func() {
Expect(testSink.Logs()[0].Data["session"]).To(Equal("1"))
Expect(testSink.Logs()[1].Data["session"]).To(Equal("1"))
Expect(testSink.Logs()[2].Data["session"]).To(Equal("1"))
Expect(testSink.Logs()[3].Data["session"]).To(Equal("1"))
})
It("logs with the task added to the message", func() {
Expect(testSink.Logs()[0].Message).To(Equal("my-component.sub-action.some-debug-action"))
Expect(testSink.Logs()[1].Message).To(Equal("my-component.sub-action.some-info-action"))
Expect(testSink.Logs()[2].Message).To(Equal("my-component.sub-action.some-error-action"))
Expect(testSink.Logs()[3].Message).To(Equal("my-component.sub-action.some-fatal-action"))
})
It("logs with the original data", func() {
Expect(testSink.Logs()[0].Data["level"]).To(Equal("debug"))
Expect(testSink.Logs()[1].Data["level"]).To(Equal("info"))
Expect(testSink.Logs()[2].Data["level"]).To(Equal("error"))
Expect(testSink.Logs()[3].Data["level"]).To(Equal("fatal"))
})
Context("with data", func() {
BeforeEach(func() {
session = logger.Session("sub-action", lager.Data{"foo": "bar"})
})
It("logs with the data added to the message", func() {
Expect(testSink.Logs()[0].Data["foo"]).To(Equal("bar"))
Expect(testSink.Logs()[1].Data["foo"]).To(Equal("bar"))
Expect(testSink.Logs()[2].Data["foo"]).To(Equal("bar"))
Expect(testSink.Logs()[3].Data["foo"]).To(Equal("bar"))
})
It("keeps the original data", func() {
Expect(testSink.Logs()[0].Data["level"]).To(Equal("debug"))
Expect(testSink.Logs()[1].Data["level"]).To(Equal("info"))
Expect(testSink.Logs()[2].Data["level"]).To(Equal("error"))
Expect(testSink.Logs()[3].Data["level"]).To(Equal("fatal"))
})
})
Context("with another session", func() {
BeforeEach(func() {
session = logger.Session("next-sub-action")
})
It("logs with a shared session id in the data", func() {
Expect(testSink.Logs()[0].Data["session"]).To(Equal("2"))
Expect(testSink.Logs()[1].Data["session"]).To(Equal("2"))
Expect(testSink.Logs()[2].Data["session"]).To(Equal("2"))
Expect(testSink.Logs()[3].Data["session"]).To(Equal("2"))
})
It("logs with the task added to the message", func() {
Expect(testSink.Logs()[0].Message).To(Equal("my-component.next-sub-action.some-debug-action"))
Expect(testSink.Logs()[1].Message).To(Equal("my-component.next-sub-action.some-info-action"))
Expect(testSink.Logs()[2].Message).To(Equal("my-component.next-sub-action.some-error-action"))
Expect(testSink.Logs()[3].Message).To(Equal("my-component.next-sub-action.some-fatal-action"))
})
})
Describe("WithData", func() {
BeforeEach(func() {
session = logger.WithData(lager.Data{"foo": "bar"})
})
It("returns a new logger with the given data", func() {
Expect(testSink.Logs()[0].Data["foo"]).To(Equal("bar"))
Expect(testSink.Logs()[1].Data["foo"]).To(Equal("bar"))
Expect(testSink.Logs()[2].Data["foo"]).To(Equal("bar"))
Expect(testSink.Logs()[3].Data["foo"]).To(Equal("bar"))
})
It("does not append to the logger's task", func() {
Expect(testSink.Logs()[0].Message).To(Equal("my-component.some-debug-action"))
})
})
Context("with a nested session", func() {
BeforeEach(func() {
session = session.Session("sub-sub-action")
})
It("logs with a shared session id in the data", func() {
Expect(testSink.Logs()[0].Data["session"]).To(Equal("1.1"))
Expect(testSink.Logs()[1].Data["session"]).To(Equal("1.1"))
Expect(testSink.Logs()[2].Data["session"]).To(Equal("1.1"))
Expect(testSink.Logs()[3].Data["session"]).To(Equal("1.1"))
})
It("logs with the task added to the message", func() {
Expect(testSink.Logs()[0].Message).To(Equal("my-component.sub-action.sub-sub-action.some-debug-action"))
Expect(testSink.Logs()[1].Message).To(Equal("my-component.sub-action.sub-sub-action.some-info-action"))
Expect(testSink.Logs()[2].Message).To(Equal("my-component.sub-action.sub-sub-action.some-error-action"))
Expect(testSink.Logs()[3].Message).To(Equal("my-component.sub-action.sub-sub-action.some-fatal-action"))
})
})
})
})
Describe("Debug", func() {
Context("with log data", func() {
BeforeEach(func() {
logger.Debug(action, logData, anotherLogData)
})
TestCommonLogFeatures(lager.DEBUG)
TestLogData()
})
Context("with no log data", func() {
BeforeEach(func() {
logger.Debug(action)
})
TestCommonLogFeatures(lager.DEBUG)
})
})
Describe("Info", func() {
Context("with log data", func() {
BeforeEach(func() {
logger.Info(action, logData, anotherLogData)
})
TestCommonLogFeatures(lager.INFO)
TestLogData()
})
Context("with no log data", func() {
BeforeEach(func() {
logger.Info(action)
})
TestCommonLogFeatures(lager.INFO)
})
})
Describe("Error", func() {
var err = errors.New("oh noes!")
Context("with log data", func() {
BeforeEach(func() {
logger.Error(action, err, logData, anotherLogData)
})
TestCommonLogFeatures(lager.ERROR)
TestLogData()
It("data contains error message", func() {
Expect(testSink.Logs()[0].Data["error"]).To(Equal(err.Error()))
})
It("retains the original error values", func() {
Expect(testSink.Errors).To(Equal([]error{err}))
})
})
Context("with no log data", func() {
BeforeEach(func() {
logger.Error(action, err)
})
TestCommonLogFeatures(lager.ERROR)
It("data contains error message", func() {
Expect(testSink.Logs()[0].Data["error"]).To(Equal(err.Error()))
})
It("retains the original error values", func() {
Expect(testSink.Errors).To(Equal([]error{err}))
})
})
Context("with no error", func() {
BeforeEach(func() {
logger.Error(action, nil)
})
TestCommonLogFeatures(lager.ERROR)
It("does not contain the error message", func() {
Expect(testSink.Logs()[0].Data).NotTo(HaveKey("error"))
})
})
})
Describe("Fatal", func() {
var err = errors.New("oh noes!")
var fatalErr interface{}
Context("with log data", func() {
BeforeEach(func() {
defer func() {
fatalErr = recover()
}()
logger.Fatal(action, err, logData, anotherLogData)
})
TestCommonLogFeatures(lager.FATAL)
TestLogData()
It("data contains error message", func() {
Expect(testSink.Logs()[0].Data["error"]).To(Equal(err.Error()))
})
It("data contains stack trace", func() {
Expect(testSink.Logs()[0].Data["trace"]).NotTo(BeEmpty())
})
It("panics with the provided error", func() {
Expect(fatalErr).To(Equal(err))
})
It("retains the original error values", func() {
Expect(testSink.Errors).To(Equal([]error{err}))
})
})
Context("with no log data", func() {
BeforeEach(func() {
defer func() {
fatalErr = recover()
}()
logger.Fatal(action, err)
})
TestCommonLogFeatures(lager.FATAL)
It("data contains error message", func() {
Expect(testSink.Logs()[0].Data["error"]).To(Equal(err.Error()))
})
It("data contains stack trace", func() {
Expect(testSink.Logs()[0].Data["trace"]).NotTo(BeEmpty())
})
It("panics with the provided error", func() {
Expect(fatalErr).To(Equal(err))
})
It("retains the original error values", func() {
Expect(testSink.Errors).To(Equal([]error{err}))
})
})
Context("with no error", func() {
BeforeEach(func() {
defer func() {
fatalErr = recover()
}()
logger.Fatal(action, nil)
})
TestCommonLogFeatures(lager.FATAL)
It("does not contain the error message", func() {
Expect(testSink.Logs()[0].Data).NotTo(HaveKey("error"))
})
It("data contains stack trace", func() {
Expect(testSink.Logs()[0].Data["trace"]).NotTo(BeEmpty())
})
It("panics with the provided error (i.e. nil)", func() {
Expect(fatalErr).To(MatchError(&runtime.PanicNilError{}))
})
})
})
Describe("WithTraceInfo", func() {
var req *http.Request
BeforeEach(func() {
var err error
req, err = http.NewRequest("GET", "/foo", nil)
Expect(err).ToNot(HaveOccurred())
})
Context("when request does not contain trace id", func() {
It("does not set trace and span id", func() {
logger = logger.WithTraceInfo(req)
logger.Info("test-log")
log := testSink.Logs()[0]
Expect(log.Data).To(BeEmpty())
Expect(log.Data).To(BeEmpty())
})
})
Context("when request contains trace id", func() {
It("sets trace and span id", func() {
req.Header.Set("X-Vcap-Request-Id", "7f461654-74d1-1ee5-8367-77d85df2cdab")
logger = logger.WithTraceInfo(req)
logger.Info("test-log")
log := testSink.Logs()[0]
Expect(log.Data["trace-id"]).To(Equal("7f46165474d11ee5836777d85df2cdab"))
Expect(log.Data["span-id"]).NotTo(BeEmpty())
})
It("generates new span id", func() {
req.Header.Set("X-Vcap-Request-Id", "7f461654-74d1-1ee5-8367-77d85df2cdab")
logger = logger.WithTraceInfo(req)
logger.Info("test-log")
log1 := testSink.Logs()[0]
Expect(log1.Data["trace-id"]).To(Equal("7f46165474d11ee5836777d85df2cdab"))
Expect(log1.Data["span-id"]).NotTo(BeEmpty())
logger = logger.WithTraceInfo(req)
logger.Info("test-log")
log2 := testSink.Logs()[1]
Expect(log2.Data["trace-id"]).To(Equal("7f46165474d11ee5836777d85df2cdab"))
Expect(log2.Data["span-id"]).NotTo(BeEmpty())
Expect(log2.Data["span-id"]).NotTo(Equal(log1.Data["span-id"]))
})
})
Context("when request contains invalid trace id", func() {
It("does not set trace and span id", func() {
req.Header.Set("X-Vcap-Request-Id", "invalid-request-id")
logger = logger.WithTraceInfo(req)
logger.Info("test-log")
log := testSink.Logs()[0]
Expect(log.Data).To(BeEmpty())
Expect(log.Data).To(BeEmpty())
})
})
})
})