diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/rule/Log4j2LevelAdjuster.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/rule/Log4j2LevelAdjuster.java index 660d817c7f..142ab56476 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/rule/Log4j2LevelAdjuster.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/rule/Log4j2LevelAdjuster.java @@ -45,8 +45,6 @@ import org.springframework.util.ObjectUtils; */ public final class Log4j2LevelAdjuster implements MethodRule { - private static final Log logger = LogFactory.getLog(Log4j2LevelAdjuster.class); - private final Class[] classes; private final Level level; diff --git a/spring-integration-test-support/src/main/java/org/springframework/integration/test/util/TestUtils.java b/spring-integration-test-support/src/main/java/org/springframework/integration/test/util/TestUtils.java index 77daf65f14..35a86c7a8b 100644 --- a/spring-integration-test-support/src/main/java/org/springframework/integration/test/util/TestUtils.java +++ b/spring-integration-test-support/src/main/java/org/springframework/integration/test/util/TestUtils.java @@ -317,38 +317,14 @@ public abstract class TestUtils { Map, Level> classLevels = new HashMap<>(); for (Class cls : classes) { String className = cls.getName(); - LoggerConfig loggerConfig = config.getLoggerConfig(className); - LoggerConfig specificConfig = loggerConfig; - - // We need a specific configuration for this logger, - // otherwise we would change the level of all other loggers - // having the original configuration as parent as well - - if (!loggerConfig.getName().equals(className)) { - specificConfig = new LoggerConfig(className, loggerConfig.getLevel(), true); - specificConfig.setParent(loggerConfig); - config.addLogger(className, specificConfig); - } - + LoggerConfig specificConfig = addLoggerConfigForCategory(config, className); classLevels.put(cls, specificConfig.getLevel()); specificConfig.setLevel(level); } Map categoryLevels = new HashMap<>(); for (String category : categories) { - LoggerConfig loggerConfig = config.getLoggerConfig(category); - LoggerConfig specificConfig = loggerConfig; - - // We need a specific configuration for this logger, - // otherwise we would change the level of all other loggers - // having the original configuration as parent as well - - if (!loggerConfig.getName().equals(category)) { - specificConfig = new LoggerConfig(category, loggerConfig.getLevel(), true); - specificConfig.setParent(loggerConfig); - config.addLogger(category, specificConfig); - } - + LoggerConfig specificConfig = addLoggerConfigForCategory(config, category); categoryLevels.put(category, specificConfig.getLevel()); specificConfig.setLevel(level); } @@ -363,6 +339,22 @@ public abstract class TestUtils { return new LevelsContainer(classLevels, categoryLevels); } + private static LoggerConfig addLoggerConfigForCategory(Configuration config, String category) { + LoggerConfig loggerConfig = config.getLoggerConfig(category); + LoggerConfig specificConfig = loggerConfig; + + // We need a specific configuration for this logger, + // otherwise we would change the level of all other loggers + // having the original configuration as parent as well + + if (!loggerConfig.getName().equals(category)) { + specificConfig = new LoggerConfig(category, loggerConfig.getLevel(), true); + specificConfig.setParent(loggerConfig); + config.addLogger(category, specificConfig); + } + return specificConfig; + } + public static void revertLogLevels(String methodName, LevelsContainer container) { LOGGER.warn("++++++++++++++++++++++++++++ " + "Restoring log level setting for: " + container.classLevels.keySet() @@ -372,14 +364,14 @@ public abstract class TestUtils { LoggerContext ctx = (LoggerContext) LogManager.getContext(false); Configuration config = ctx.getConfiguration(); - container.classLevels.entrySet().forEach(entry -> { - LoggerConfig loggerConfig = config.getLoggerConfig(entry.getKey().getName()); - loggerConfig.setLevel(entry.getValue()); + container.classLevels.forEach((key, value) -> { + LoggerConfig loggerConfig = config.getLoggerConfig(key.getName()); + loggerConfig.setLevel(value); }); - container.categoryLevels.entrySet().forEach(entry -> { - LoggerConfig loggerConfig = config.getLoggerConfig(entry.getKey()); - loggerConfig.setLevel(entry.getValue()); + container.categoryLevels.forEach((key, value) -> { + LoggerConfig loggerConfig = config.getLoggerConfig(key); + loggerConfig.setLevel(value); }); ctx.updateLoggers(); @@ -387,9 +379,9 @@ public abstract class TestUtils { public static class LevelsContainer { - final Map, Level> classLevels; + private final Map, Level> classLevels; - final Map categoryLevels; + private final Map categoryLevels; public LevelsContainer(Map, Level> classLevels, Map categoryLevels) { this.classLevels = classLevels;