-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger_test.go
75 lines (59 loc) · 1.97 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
package clogger
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_newLoggerWithDecorators(t *testing.T) {
l := NewDefaultLogger()
l.SetEventOptions(WithOpenTelemetryTrace(), WithOpenCensusTrace(), WithExampleEventOption())
l.SetLogEntryOptions(WithOpenTelemetrySpan(), WithOpenCensusSpan(), WithExampleEntryOption())
SetGlobal(l)
defer SetGlobal(NewDefaultLogger())
assert.Equal(t, 3, len(l.EventOptions()))
assert.Equal(t, 3, len(l.LogEntryOptions()))
ctx := context.Background()
Info(ctx, "An Info log entry. Part 1.")
Info(ctx, "An Info log entry. Part 2.")
}
func Test_Logger(t *testing.T) {
SetGlobal(noopLogger)
defer SetGlobal(NewDefaultLogger())
ctx, ev := NewEvent(context.Background(), "A new test logger instance")
Debug(ctx, "Debug called")
Debugf(ctx, "Debugf with (%s) called", "argument")
With("logger_key", "logger_value").Info(ctx, "Info With")
With("logger_key_debug", "logger_value_debug").Debug(ctx, "Debug With")
ev.End()
assert.Equal(t, ev, ctx.Value(eventKey).(*Event))
assert.NotPanics(t, func() {
for _, l := range ev.logs {
if l.fields.retrieve("logger_key") == "logger_value" {
return
}
}
panic("could not find required key/value pair")
})
}
func Test_LoggerTerminalEnc(t *testing.T) {
l := NewDefaultLogger()
l.SetEncoder(&TerminalEncoder{})
SetGlobal(l)
defer SetGlobal(NewDefaultLogger())
ctx, ev := NewEvent(context.Background(), "A new test logger instance with terminal encoding")
Debug(ctx, "Debug called")
Debugf(ctx, "Debugf with (%s) called", "argument")
With("logger_key", "logger_value").Info(ctx, "Info With")
With("logger_key_debug", "logger_value_debug").Debug(ctx, "Debug With")
ev.Set("event_key", "event_value")
ev.End()
assert.Equal(t, ev, ctx.Value(eventKey).(*Event))
assert.NotPanics(t, func() {
for _, l := range ev.logs {
if l.fields.retrieve("logger_key_debug") == "logger_value_debug" {
return
}
}
panic("could not find required key/value pair")
})
}