-
Notifications
You must be signed in to change notification settings - Fork 9
/
default_test.go
166 lines (130 loc) · 5.83 KB
/
default_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
package slog
import (
"context"
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDefaultLogger(t *testing.T) {
logger := NewInMemoryLogger()
oldLogger := DefaultLogger()
SetDefaultLogger(logger)
defer SetDefaultLogger(oldLogger)
Trace(context.Background(), "Important trace message", "foo")
Debug(context.Background(), "Important debug message", "foo")
Info(context.Background(), "Important info message", "foo")
Warn(context.Background(), "Important warn message", "foo")
Error(context.Background(), "Important error message", "foo")
Critical(context.Background(), "Important critical message", "foo")
events := logger.Events()
require.Equal(t, 6, len(events))
assert.Equal(t, TraceSeverity, events[0].Severity)
assert.Equal(t, DebugSeverity, events[1].Severity)
assert.Equal(t, InfoSeverity, events[2].Severity)
assert.Equal(t, WarnSeverity, events[3].Severity)
assert.Equal(t, ErrorSeverity, events[4].Severity)
assert.Equal(t, CriticalSeverity, events[5].Severity)
}
func TestDefaultLoggerWithLeveledLogger(t *testing.T) {
logger := &testLogLeveledLogger{t: t}
oldLogger := DefaultLogger()
SetDefaultLogger(logger)
defer SetDefaultLogger(oldLogger)
Trace(context.Background(), "Important trace message", "foo")
Debug(context.Background(), "Important debug message", "foo")
Info(context.Background(), "Important info message", "foo")
Warn(context.Background(), "Important warn message", "foo")
Error(context.Background(), "Important error message", "foo")
Critical(context.Background(), "Important critical message", "foo")
require.Equal(t, 6, len(logger.items))
assert.Equal(t, TraceSeverity, logger.items[0].Severity)
assert.Equal(t, "Important trace message", logger.items[0].OriginalMessage)
assert.Equal(t, DebugSeverity, logger.items[1].Severity)
assert.Equal(t, "Important debug message", logger.items[1].OriginalMessage)
assert.Equal(t, InfoSeverity, logger.items[2].Severity)
assert.Equal(t, "Important info message", logger.items[2].OriginalMessage)
assert.Equal(t, WarnSeverity, logger.items[3].Severity)
assert.Equal(t, "Important warn message", logger.items[3].OriginalMessage)
assert.Equal(t, ErrorSeverity, logger.items[4].Severity)
assert.Equal(t, "Important error message", logger.items[4].OriginalMessage)
assert.Equal(t, CriticalSeverity, logger.items[5].Severity)
assert.Equal(t, "Important critical message", logger.items[5].OriginalMessage)
}
func TestDefaultLoggerWithFromErrorLogger(t *testing.T) {
logger := &testLogFromErrorLogger{t: t}
oldLogger := DefaultLogger()
SetDefaultLogger(logger)
defer SetDefaultLogger(oldLogger)
FromError(context.Background(), "This error ends up as debug", context.Canceled, "foo")
FromError(context.Background(), "This error ends up as error", context.DeadlineExceeded, "foo")
require.Equal(t, 2, len(logger.items))
assert.Equal(t, DebugSeverity, logger.items[0].Severity)
assert.Equal(t, "This error ends up as debug", logger.items[0].OriginalMessage)
assert.Equal(t, ErrorSeverity, logger.items[1].Severity)
assert.Equal(t, "This error ends up as error", logger.items[1].OriginalMessage)
}
func TestNilDefaultLogger(t *testing.T) {
oldLogger := DefaultLogger()
SetDefaultLogger(nil)
defer SetDefaultLogger(oldLogger)
require.Nil(t, DefaultLogger())
Trace(context.Background(), "Important trace message", "foo")
Debug(context.Background(), "Important debug message", "foo")
Info(context.Background(), "Important info message", "foo")
Warn(context.Background(), "Important warn message", "foo")
Error(context.Background(), "Important error message", "foo")
Critical(context.Background(), "Important critical message", "foo")
FromError(context.Background(), "Important from error message", context.Canceled, "foo")
}
type logItem struct {
Severity Severity
OriginalMessage string
}
// testLogLeveledLogger implements the Logger and LeveledLogger interfaces
type testLogLeveledLogger struct {
t *testing.T
items []logItem
}
func (l *testLogLeveledLogger) Critical(ctx context.Context, msg string, params ...interface{}) {
l.items = append(l.items, logItem{Severity: CriticalSeverity, OriginalMessage: msg})
}
func (l *testLogLeveledLogger) Error(ctx context.Context, msg string, params ...interface{}) {
l.items = append(l.items, logItem{Severity: ErrorSeverity, OriginalMessage: msg})
}
func (l *testLogLeveledLogger) Warn(ctx context.Context, msg string, params ...interface{}) {
l.items = append(l.items, logItem{Severity: WarnSeverity, OriginalMessage: msg})
}
func (l *testLogLeveledLogger) Info(ctx context.Context, msg string, params ...interface{}) {
l.items = append(l.items, logItem{Severity: InfoSeverity, OriginalMessage: msg})
}
func (l *testLogLeveledLogger) Debug(ctx context.Context, msg string, params ...interface{}) {
l.items = append(l.items, logItem{Severity: DebugSeverity, OriginalMessage: msg})
}
func (l *testLogLeveledLogger) Trace(ctx context.Context, msg string, params ...interface{}) {
l.items = append(l.items, logItem{Severity: TraceSeverity, OriginalMessage: msg})
}
func (l *testLogLeveledLogger) Log(evs ...Event) {
l.t.Fail() // We expect this method to not be called
}
func (l *testLogLeveledLogger) Flush() error {
return nil
}
// testLogFromErrorLogger implements the Logger and FromErrorLogger interfaces
type testLogFromErrorLogger struct {
t *testing.T
items []logItem
}
func (l *testLogFromErrorLogger) FromError(ctx context.Context, msg string, err error, params ...interface{}) {
if errors.Is(err, context.Canceled) {
l.items = append(l.items, logItem{Severity: DebugSeverity, OriginalMessage: msg})
return
}
l.items = append(l.items, logItem{Severity: ErrorSeverity, OriginalMessage: msg})
}
func (l *testLogFromErrorLogger) Log(evs ...Event) {
l.t.Fail() // We expect this method to not be called
}
func (l *testLogFromErrorLogger) Flush() error {
return nil
}