@@ -148,29 +148,24 @@ func TestGetModuleLevelDefault(t *testing.T) {
148
148
level , _ := flogging .GetModuleLevel ("peer" )
149
149
150
150
// peer should be using the default log level at this point
151
- if level != "INFO" {
152
- t .FailNow ()
153
- }
151
+ assertEquals (t , logging .INFO .String (), level )
152
+
154
153
}
155
154
156
155
func TestGetModuleLevelDebug (t * testing.T ) {
157
156
flogging .SetModuleLevel ("peer" , "DEBUG" )
158
157
level , _ := flogging .GetModuleLevel ("peer" )
159
158
160
159
// ensure that the log level has changed to debug
161
- if level != "DEBUG" {
162
- t .FailNow ()
163
- }
160
+ assertEquals (t , logging .DEBUG .String (), level )
164
161
}
165
162
166
163
func TestGetModuleLevelInvalid (t * testing.T ) {
167
164
flogging .SetModuleLevel ("peer" , "invalid" )
168
165
level , _ := flogging .GetModuleLevel ("peer" )
169
166
170
167
// ensure that the log level didn't change after invalid log level specified
171
- if level != "DEBUG" {
172
- t .FailNow ()
173
- }
168
+ assertEquals (t , logging .DEBUG .String (), level )
174
169
}
175
170
176
171
func TestSetModuleLevel (t * testing.T ) {
@@ -217,6 +212,145 @@ func ExampleSetLoggingFormat_second() {
217
212
// 00:00:00.000 [floggingTest] ExampleSetLoggingFormat_second -> INFO 001 test
218
213
}
219
214
215
+ // TestSetModuleLevel_moduleWithSubmodule and the following tests use the new
216
+ // flogging.MustGetLogger to initialize their loggers. flogging.MustGetLogger
217
+ // adds a modules map to track which module names have been defined and their
218
+ // current log level
219
+ func TestSetModuleLevel_moduleWithSubmodule (t * testing.T ) {
220
+ // enable setting the level by regular expressions
221
+ // TODO - remove this once all modules have been updated to use
222
+ // flogging.MustGetLogger
223
+ flogging .IsSetLevelByRegExpEnabled = true
224
+
225
+ flogging .MustGetLogger ("module" )
226
+ flogging .MustGetLogger ("module/subcomponent" )
227
+ flogging .MustGetLogger ("sub" )
228
+
229
+ // ensure that the log level is the default level, INFO
230
+ assertModuleLevel (t , "module" , logging .INFO )
231
+ assertModuleLevel (t , "module/subcomponent" , logging .INFO )
232
+ assertModuleLevel (t , "sub" , logging .INFO )
233
+
234
+ // set level for modules that contain 'module'
235
+ flogging .SetModuleLevel ("module" , "warning" )
236
+
237
+ // ensure that the log level has changed to WARNING for the modules
238
+ // that match the regular expression
239
+ assertModuleLevel (t , "module" , logging .WARNING )
240
+ assertModuleLevel (t , "module/subcomponent" , logging .WARNING )
241
+ assertModuleLevel (t , "sub" , logging .INFO )
242
+
243
+ flogging .IsSetLevelByRegExpEnabled = false
244
+ }
245
+
246
+ func TestSetModuleLevel_regExpOr (t * testing.T ) {
247
+ flogging .IsSetLevelByRegExpEnabled = true
248
+
249
+ flogging .MustGetLogger ("module2" )
250
+ flogging .MustGetLogger ("module2/subcomponent" )
251
+ flogging .MustGetLogger ("sub2" )
252
+ flogging .MustGetLogger ("randomLogger2" )
253
+
254
+ // ensure that the log level is the default level, INFO
255
+ assertModuleLevel (t , "module2" , logging .INFO )
256
+ assertModuleLevel (t , "module2/subcomponent" , logging .INFO )
257
+ assertModuleLevel (t , "sub2" , logging .INFO )
258
+ assertModuleLevel (t , "randomLogger2" , logging .INFO )
259
+
260
+ // set level for modules that contain 'mod' OR 'sub'
261
+ flogging .SetModuleLevel ("mod|sub" , "DEBUG" )
262
+
263
+ // ensure that the log level has changed to DEBUG for the modules that match
264
+ // the regular expression
265
+ assertModuleLevel (t , "module2" , logging .DEBUG )
266
+ assertModuleLevel (t , "module2/subcomponent" , logging .DEBUG )
267
+ assertModuleLevel (t , "sub2" , logging .DEBUG )
268
+ assertModuleLevel (t , "randomLogger2" , logging .INFO )
269
+
270
+ flogging .IsSetLevelByRegExpEnabled = false
271
+ }
272
+
273
+ func TestSetModuleLevel_regExpSuffix (t * testing.T ) {
274
+ flogging .IsSetLevelByRegExpEnabled = true
275
+
276
+ flogging .MustGetLogger ("module3" )
277
+ flogging .MustGetLogger ("module3/subcomponent" )
278
+ flogging .MustGetLogger ("sub3" )
279
+ flogging .MustGetLogger ("sub3/subcomponent" )
280
+ flogging .MustGetLogger ("randomLogger3" )
281
+
282
+ // ensure that the log level is the default level, INFO
283
+ assertModuleLevel (t , "module3" , logging .INFO )
284
+ assertModuleLevel (t , "module3/subcomponent" , logging .INFO )
285
+ assertModuleLevel (t , "sub3" , logging .INFO )
286
+ assertModuleLevel (t , "sub3/subcomponent" , logging .INFO )
287
+ assertModuleLevel (t , "randomLogger3" , logging .INFO )
288
+
289
+ // set level for modules that contain component
290
+ flogging .SetModuleLevel ("component$" , "ERROR" )
291
+
292
+ // ensure that the log level has changed to ERROR for the modules that match
293
+ // the regular expression
294
+ assertModuleLevel (t , "module3" , logging .INFO )
295
+ assertModuleLevel (t , "module3/subcomponent" , logging .ERROR )
296
+ assertModuleLevel (t , "sub3" , logging .INFO )
297
+ assertModuleLevel (t , "sub3/subcomponent" , logging .ERROR )
298
+ assertModuleLevel (t , "randomLogger3" , logging .INFO )
299
+
300
+ flogging .IsSetLevelByRegExpEnabled = false
301
+ }
302
+
303
+ func TestSetModuleLevel_regExpComplex (t * testing.T ) {
304
+ flogging .IsSetLevelByRegExpEnabled = true
305
+
306
+ flogging .MustGetLogger ("gossip/util" )
307
+ flogging .MustGetLogger ("orderer/util" )
308
+ flogging .MustGetLogger ("gossip/gossip#0.0.0.0:7051" )
309
+ flogging .MustGetLogger ("gossip/conn#-1" )
310
+ flogging .MustGetLogger ("orderer/conn#0.0.0.0:7051" )
311
+
312
+ // ensure that the log level is the default level, INFO
313
+ assertModuleLevel (t , "gossip/util" , logging .INFO )
314
+ assertModuleLevel (t , "orderer/util" , logging .INFO )
315
+ assertModuleLevel (t , "gossip/gossip#0.0.0.0:7051" , logging .INFO )
316
+ assertModuleLevel (t , "gossip/conn#-1" , logging .INFO )
317
+ assertModuleLevel (t , "orderer/conn#0.0.0.0:7051" , logging .INFO )
318
+
319
+ // set level for modules that match the regular rexpression
320
+ flogging .SetModuleLevel ("^[a-z]+\\ /[a-z]+#.+$" , "WARNING" )
321
+
322
+ // ensure that the log level has changed to WARNING for the modules that match
323
+ // the regular expression
324
+ assertModuleLevel (t , "gossip/util" , logging .INFO )
325
+ assertModuleLevel (t , "orderer/util" , logging .INFO )
326
+ assertModuleLevel (t , "gossip/gossip#0.0.0.0:7051" , logging .WARNING )
327
+ assertModuleLevel (t , "gossip/conn#-1" , logging .WARNING )
328
+ assertModuleLevel (t , "orderer/conn#0.0.0.0:7051" , logging .WARNING )
329
+
330
+ flogging .IsSetLevelByRegExpEnabled = false
331
+ }
332
+
333
+ func TestSetModuleLevel_invalidRegExp (t * testing.T ) {
334
+ flogging .IsSetLevelByRegExpEnabled = true
335
+
336
+ flogging .MustGetLogger ("(module" )
337
+
338
+ // ensure that the log level is the default level, INFO
339
+ assertModuleLevel (t , "(module" , logging .INFO )
340
+
341
+ level , err := flogging .SetModuleLevel ("(" , "info" )
342
+
343
+ assertEquals (t , "" , level )
344
+ if err == nil {
345
+ t .FailNow ()
346
+ }
347
+
348
+ // ensure that the log level hasn't changed
349
+ assertModuleLevel (t , "(module" , logging .INFO )
350
+
351
+ flogging .IsSetLevelByRegExpEnabled = false
352
+ }
353
+
220
354
func assertDefaultLevel (t * testing.T , expectedLevel logging.Level ) {
221
355
assertModuleLevel (t , "" , expectedLevel )
222
356
}
0 commit comments