-
Notifications
You must be signed in to change notification settings - Fork 1
/
log_test.go
303 lines (243 loc) · 9.94 KB
/
log_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
package logger_test
import (
"fmt"
"strings"
"testing"
logger "git.sonicoriginal.software/logger.git"
)
const (
defaultPrefix = "Test"
testMessageError = "Test error message"
testMessageWarn = "Test warn message"
testMessageInfo = "Test info message"
testMessageDebug = "Test debug message"
enabledValue = "1"
undeterminedValue = "-1"
bogusValue = "bogus"
disabledValue = "0"
)
type callback func(f string, v ...any)
func prepare(severity logger.Severity, prefix string) (writer *strings.Builder, testLogger *logger.Logger) {
writer = &strings.Builder{}
testLogger = logger.New(prefix, severity, writer, writer)
return
}
func runTest(
t *testing.T,
testLogger *logger.Logger,
testCall callback,
channel logger.Severity,
message string,
channelLabel logger.ChannelLabel,
writer *strings.Builder,
) {
testCall(message)
if !testLogger.ChannelEnabled(channel) {
return
}
receivedMessage := writer.String()
expectedSuffix := fmt.Sprintf("[%v] [%v] %v\n", channelLabel, defaultPrefix, message)
if !strings.HasSuffix(receivedMessage, expectedSuffix) {
t.Errorf("'%v' != '%v'", message, receivedMessage)
}
}
func testError(t *testing.T, loggerSeverity logger.Severity, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
writer, testLogger := prepare(loggerSeverity, prefix)
testFunction := testLogger.Error
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func testWarn(t *testing.T, loggerSeverity logger.Severity, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
writer, testLogger := prepare(loggerSeverity, prefix)
testFunction := testLogger.Warn
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func testInfo(t *testing.T, loggerSeverity logger.Severity, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
writer, testLogger := prepare(loggerSeverity, prefix)
testFunction := testLogger.Info
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func testDebug(t *testing.T, loggerSeverity logger.Severity, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
writer, testLogger := prepare(loggerSeverity, prefix)
testFunction := testLogger.Debug
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func logLevelError(t *testing.T, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
writer, testLogger := prepare(logger.DefaultSeverity, prefix)
testFunction := testLogger.Error
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func logLevelWarn(t *testing.T, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
writer, testLogger := prepare(logger.DefaultSeverity, prefix)
testFunction := testLogger.Warn
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func logLevelInfo(t *testing.T, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
writer, testLogger := prepare(logger.DefaultSeverity, prefix)
testFunction := testLogger.Info
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func logLevelDebug(t *testing.T, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
writer, testLogger := prepare(logger.DefaultSeverity, prefix)
testFunction := testLogger.Debug
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func prefixedLogger(t *testing.T, channel logger.Severity, channelLabel logger.ChannelLabel, prefix, message string) {
loggerBDebugLogLevel := fmt.Sprintf("%v_LOG_LEVEL_%v", prefix, logger.ChannelLabelDebug)
t.Setenv(loggerBDebugLogLevel, enabledValue)
writer := &strings.Builder{}
testLogger := logger.New(defaultPrefix, logger.DefaultSeverity, writer, writer)
testFunction := testLogger.Debug
runTest(t, testLogger, testFunction, channel, message, channelLabel, writer)
}
func TestError(t *testing.T) {
channel := logger.Error
channelLabel := logger.ChannelLabelError
message := testMessageError
t.Run("Default Error", func(t *testing.T) {
testError(t, logger.DefaultSeverity, channel, channelLabel, defaultPrefix, message)
})
t.Run("Only Error", func(t *testing.T) {
testError(t, logger.Error, channel, channelLabel, defaultPrefix, message)
})
t.Run("Except Error", func(t *testing.T) {
testError(t, logger.Warn|logger.Info|logger.Debug, channel, channelLabel, defaultPrefix, message)
})
}
func TestWarn(t *testing.T) {
channel := logger.Warn
channelLabel := logger.ChannelLabelWarn
message := testMessageWarn
t.Run("Default Warn", func(t *testing.T) {
testWarn(t, logger.DefaultSeverity, channel, channelLabel, defaultPrefix, message)
})
t.Run("Only Warn", func(t *testing.T) {
testWarn(t, logger.Warn, channel, channelLabel, defaultPrefix, message)
})
t.Run("Except Warn", func(t *testing.T) {
testWarn(t, logger.Error|logger.Info|logger.Debug, channel, channelLabel, defaultPrefix, message)
})
}
func TestInfo(t *testing.T) {
channel := logger.Info
channelLabel := logger.ChannelLabelInfo
message := testMessageInfo
t.Run("Default Info", func(t *testing.T) {
testInfo(t, logger.DefaultSeverity, channel, channelLabel, defaultPrefix, message)
})
t.Run("Only Info", func(t *testing.T) {
testInfo(t, logger.Info, channel, channelLabel, defaultPrefix, message)
})
t.Run("Except Info", func(t *testing.T) {
testInfo(t, logger.Error|logger.Warn|logger.Debug, channel, channelLabel, defaultPrefix, message)
})
}
func TestDebug(t *testing.T) {
channel := logger.Debug
channelLabel := logger.ChannelLabelDebug
message := testMessageDebug
t.Run("Default Debug", func(t *testing.T) {
testDebug(t, logger.DefaultSeverity, channel, channelLabel, defaultPrefix, message)
})
t.Run("Only Debug", func(t *testing.T) {
testDebug(t, logger.Debug, channel, channelLabel, defaultPrefix, message)
})
t.Run("Except Debug", func(t *testing.T) {
testDebug(t, logger.Error|logger.Warn|logger.Info, channel, channelLabel, defaultPrefix, message)
})
}
func TestLogLevelError(t *testing.T) {
channel := logger.Error
channelLabel := logger.ChannelLabelError
envVariable := fmt.Sprintf("LOG_LEVEL_%v", channelLabel)
message := testMessageError
t.Run("Default Severity Error LogLevel Disabled", func(t *testing.T) {
t.Setenv(envVariable, disabledValue)
logLevelError(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Error LogLevel Enabled", func(t *testing.T) {
t.Setenv(envVariable, enabledValue)
logLevelError(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Error LogLevel Undetermined", func(t *testing.T) {
t.Setenv(envVariable, undeterminedValue)
logLevelError(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Error LogLevel Bogus", func(t *testing.T) {
t.Setenv(envVariable, bogusValue)
logLevelError(t, channel, channelLabel, defaultPrefix, message)
})
}
func TestLogLevelWarn(t *testing.T) {
channel := logger.Warn
channelLabel := logger.ChannelLabelWarn
envVariable := fmt.Sprintf("LOG_LEVEL_%v", channelLabel)
message := testMessageWarn
t.Run("Default Severity Warn LogLevel Disabled", func(t *testing.T) {
t.Setenv(envVariable, disabledValue)
logLevelWarn(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Warn LogLevel Enabled", func(t *testing.T) {
t.Setenv(envVariable, enabledValue)
logLevelWarn(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Warn LogLevel Undetermined", func(t *testing.T) {
t.Setenv(envVariable, undeterminedValue)
logLevelWarn(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Warn LogLevel Bogus", func(t *testing.T) {
t.Setenv(envVariable, bogusValue)
logLevelWarn(t, channel, channelLabel, defaultPrefix, message)
})
}
func TestLogLevelInfo(t *testing.T) {
channel := logger.Info
channelLabel := logger.ChannelLabelInfo
envVariable := fmt.Sprintf("LOG_LEVEL_%v", channelLabel)
message := testMessageInfo
t.Run("Default Severity Info LogLevel Disabled", func(t *testing.T) {
t.Setenv(envVariable, disabledValue)
logLevelInfo(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Info LogLevel Enabled", func(t *testing.T) {
t.Setenv(envVariable, enabledValue)
logLevelInfo(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Info LogLevel Undetermined", func(t *testing.T) {
t.Setenv(envVariable, undeterminedValue)
logLevelInfo(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Info LogLevel Bogus", func(t *testing.T) {
t.Setenv(envVariable, bogusValue)
logLevelInfo(t, channel, channelLabel, defaultPrefix, message)
})
}
func TestLogLevelDebug(t *testing.T) {
channel := logger.Debug
channelLabel := logger.ChannelLabelDebug
envVariable := fmt.Sprintf("LOG_LEVEL_%v", channelLabel)
message := testMessageDebug
t.Run("Default Severity Debug LogLevel Disabled", func(t *testing.T) {
t.Setenv(envVariable, disabledValue)
logLevelDebug(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Debug LogLevel Enabled", func(t *testing.T) {
t.Setenv(envVariable, enabledValue)
logLevelDebug(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Debug LogLevel Undetermined", func(t *testing.T) {
t.Setenv(envVariable, undeterminedValue)
logLevelDebug(t, channel, channelLabel, defaultPrefix, message)
})
t.Run("Default Severity Debug LogLevel Bogus", func(t *testing.T) {
t.Setenv(envVariable, bogusValue)
logLevelDebug(t, channel, channelLabel, defaultPrefix, message)
})
}
func TestLoggerLogLevelPrefix(t *testing.T) {
channel := logger.Debug
channelLabel := logger.ChannelLabelDebug
message := testMessageDebug
t.Run("Default Severity Prefixed Logger", func(t *testing.T) {
prefixedLogger(t, channel, channelLabel, defaultPrefix, message)
})
}