@@ -317,38 +317,14 @@ public static LevelsContainer adjustLogLevels(String methodName, List<Class<?>>
317
317
Map <Class <?>, Level > classLevels = new HashMap <>();
318
318
for (Class <?> cls : classes ) {
319
319
String className = cls .getName ();
320
- LoggerConfig loggerConfig = config .getLoggerConfig (className );
321
- LoggerConfig specificConfig = loggerConfig ;
322
-
323
- // We need a specific configuration for this logger,
324
- // otherwise we would change the level of all other loggers
325
- // having the original configuration as parent as well
326
-
327
- if (!loggerConfig .getName ().equals (className )) {
328
- specificConfig = new LoggerConfig (className , loggerConfig .getLevel (), true );
329
- specificConfig .setParent (loggerConfig );
330
- config .addLogger (className , specificConfig );
331
- }
332
-
320
+ LoggerConfig specificConfig = addLoggerConfigForCategory (config , className );
333
321
classLevels .put (cls , specificConfig .getLevel ());
334
322
specificConfig .setLevel (level );
335
323
}
336
324
337
325
Map <String , Level > categoryLevels = new HashMap <>();
338
326
for (String category : categories ) {
339
- LoggerConfig loggerConfig = config .getLoggerConfig (category );
340
- LoggerConfig specificConfig = loggerConfig ;
341
-
342
- // We need a specific configuration for this logger,
343
- // otherwise we would change the level of all other loggers
344
- // having the original configuration as parent as well
345
-
346
- if (!loggerConfig .getName ().equals (category )) {
347
- specificConfig = new LoggerConfig (category , loggerConfig .getLevel (), true );
348
- specificConfig .setParent (loggerConfig );
349
- config .addLogger (category , specificConfig );
350
- }
351
-
327
+ LoggerConfig specificConfig = addLoggerConfigForCategory (config , category );
352
328
categoryLevels .put (category , specificConfig .getLevel ());
353
329
specificConfig .setLevel (level );
354
330
}
@@ -363,6 +339,22 @@ public static LevelsContainer adjustLogLevels(String methodName, List<Class<?>>
363
339
return new LevelsContainer (classLevels , categoryLevels );
364
340
}
365
341
342
+ private static LoggerConfig addLoggerConfigForCategory (Configuration config , String category ) {
343
+ LoggerConfig loggerConfig = config .getLoggerConfig (category );
344
+ LoggerConfig specificConfig = loggerConfig ;
345
+
346
+ // We need a specific configuration for this logger,
347
+ // otherwise we would change the level of all other loggers
348
+ // having the original configuration as parent as well
349
+
350
+ if (!loggerConfig .getName ().equals (category )) {
351
+ specificConfig = new LoggerConfig (category , loggerConfig .getLevel (), true );
352
+ specificConfig .setParent (loggerConfig );
353
+ config .addLogger (category , specificConfig );
354
+ }
355
+ return specificConfig ;
356
+ }
357
+
366
358
public static void revertLogLevels (String methodName , LevelsContainer container ) {
367
359
LOGGER .warn ("++++++++++++++++++++++++++++ "
368
360
+ "Restoring log level setting for: " + container .classLevels .keySet ()
@@ -372,24 +364,24 @@ public static void revertLogLevels(String methodName, LevelsContainer container)
372
364
LoggerContext ctx = (LoggerContext ) LogManager .getContext (false );
373
365
Configuration config = ctx .getConfiguration ();
374
366
375
- container .classLevels .entrySet (). forEach (entry -> {
376
- LoggerConfig loggerConfig = config .getLoggerConfig (entry . getKey () .getName ());
377
- loggerConfig .setLevel (entry . getValue () );
367
+ container .classLevels .forEach (( key , value ) -> {
368
+ LoggerConfig loggerConfig = config .getLoggerConfig (key .getName ());
369
+ loggerConfig .setLevel (value );
378
370
});
379
371
380
- container .categoryLevels .entrySet (). forEach (entry -> {
381
- LoggerConfig loggerConfig = config .getLoggerConfig (entry . getKey () );
382
- loggerConfig .setLevel (entry . getValue () );
372
+ container .categoryLevels .forEach (( key , value ) -> {
373
+ LoggerConfig loggerConfig = config .getLoggerConfig (key );
374
+ loggerConfig .setLevel (value );
383
375
});
384
376
385
377
ctx .updateLoggers ();
386
378
}
387
379
388
380
public static class LevelsContainer {
389
381
390
- final Map <Class <?>, Level > classLevels ;
382
+ private final Map <Class <?>, Level > classLevels ;
391
383
392
- final Map <String , Level > categoryLevels ;
384
+ private final Map <String , Level > categoryLevels ;
393
385
394
386
public LevelsContainer (Map <Class <?>, Level > classLevels , Map <String , Level > categoryLevels ) {
395
387
this .classLevels = classLevels ;
0 commit comments