diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java index 711e180e64..e84f025026 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/AbstractLoggingSystem.java @@ -16,6 +16,7 @@ package org.springframework.boot.logging; +import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; @@ -35,6 +36,9 @@ import org.springframework.util.SystemPropertyUtils; */ public abstract class AbstractLoggingSystem extends LoggingSystem { + protected static final Comparator CONFIGURATION_COMPARATOR = new LoggerConfigurationComparator( + ROOT_LOGGER_NAME); + private final ClassLoader classLoader; public AbstractLoggingSystem(ClassLoader classLoader) { diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggerConfigurationComparator.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggerConfigurationComparator.java index ae0cb44095..dee904a508 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggerConfigurationComparator.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggerConfigurationComparator.java @@ -25,9 +25,8 @@ import org.springframework.util.Assert; * Sorts the "root" logger as the first logger and then lexically by name after that. * * @author Ben Hale - * @since 1.5.0 */ -public class LoggerConfigurationComparator implements Comparator { +class LoggerConfigurationComparator implements Comparator { private final String rootLoggerName; @@ -35,7 +34,7 @@ public class LoggerConfigurationComparator implements Comparator SYSTEMS; static { @@ -107,7 +114,8 @@ public abstract class LoggingSystem { /** * Sets the logging level for a given logger. - * @param loggerName the name of the logger to set + * @param loggerName the name of the logger to set ({@code null} can be used for the + * root logger). * @param level the log level */ public void setLogLevel(String loggerName, LogLevel level) { diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java index da05c83f7f..451afd5ff9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/java/JavaLoggingSystem.java @@ -31,7 +31,6 @@ import org.springframework.boot.logging.AbstractLoggingSystem; import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggerConfiguration; -import org.springframework.boot.logging.LoggerConfigurationComparator; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; import org.springframework.util.Assert; @@ -49,9 +48,6 @@ import org.springframework.util.StringUtils; */ public class JavaLoggingSystem extends AbstractLoggingSystem { - private static final LoggerConfigurationComparator COMPARATOR = new LoggerConfigurationComparator( - ""); - private static final LogLevels LEVELS = new LogLevels(); static { @@ -122,8 +118,10 @@ public class JavaLoggingSystem extends AbstractLoggingSystem { @Override public void setLogLevel(String loggerName, LogLevel level) { Assert.notNull(level, "Level must not be null"); - String name = (StringUtils.hasText(loggerName) ? loggerName : ""); - Logger logger = Logger.getLogger(name); + if (loggerName == null || ROOT_LOGGER_NAME.equals(loggerName)) { + loggerName = ""; + } + Logger logger = Logger.getLogger(loggerName); if (logger != null) { logger.setLevel(LEVELS.convertSystemToNative(level)); } @@ -136,7 +134,7 @@ public class JavaLoggingSystem extends AbstractLoggingSystem { while (names.hasMoreElements()) { result.add(getLoggerConfiguration(names.nextElement())); } - Collections.sort(result, COMPARATOR); + Collections.sort(result, CONFIGURATION_COMPARATOR); return Collections.unmodifiableList(result); } @@ -148,7 +146,9 @@ public class JavaLoggingSystem extends AbstractLoggingSystem { } LogLevel level = LEVELS.convertNativeToSystem(logger.getLevel()); LogLevel effectiveLevel = LEVELS.convertNativeToSystem(getEffectiveLevel(logger)); - return new LoggerConfiguration(logger.getName(), level, effectiveLevel); + String name = (StringUtils.hasLength(logger.getName()) ? logger.getName() + : ROOT_LOGGER_NAME); + return new LoggerConfiguration(name, level, effectiveLevel); } private Level getEffectiveLevel(Logger root) { diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index 3ccc3c62fe..41f966cc5d 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -41,7 +41,6 @@ import org.apache.logging.log4j.message.Message; import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggerConfiguration; -import org.springframework.boot.logging.LoggerConfigurationComparator; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.Slf4JLoggingSystem; @@ -61,9 +60,6 @@ import org.springframework.util.StringUtils; */ public class Log4J2LoggingSystem extends Slf4JLoggingSystem { - private static final LoggerConfigurationComparator COMPARATOR = new LoggerConfigurationComparator( - LogManager.ROOT_LOGGER_NAME); - private static final String FILE_PROTOCOL = "file"; private static final LogLevels LEVELS = new LogLevels(); @@ -224,7 +220,7 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem { for (LoggerConfig loggerConfig : configuration.getLoggers().values()) { result.add(convertLoggerConfiguration(loggerConfig)); } - Collections.sort(result, COMPARATOR); + Collections.sort(result, CONFIGURATION_COMPARATOR); return result; } @@ -238,7 +234,11 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem { return null; } LogLevel level = LEVELS.convertNativeToSystem(loggerConfig.getLevel()); - return new LoggerConfiguration(loggerConfig.getName(), level, level); + String name = loggerConfig.getName(); + if (!StringUtils.hasLength(name) || LogManager.ROOT_LOGGER_NAME.equals(name)) { + name = ROOT_LOGGER_NAME; + } + return new LoggerConfiguration(name, level, level); } @Override @@ -254,7 +254,9 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem { } private LoggerConfig getLoggerConfig(String name) { - name = (StringUtils.hasText(name) ? name : LogManager.ROOT_LOGGER_NAME); + if (!StringUtils.hasLength(name) || ROOT_LOGGER_NAME.equals(name)) { + name = LogManager.ROOT_LOGGER_NAME; + } return getLoggerContext().getConfiguration().getLoggers().get(name); } diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java index c71971466e..28a5d90543 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java @@ -41,7 +41,6 @@ import org.slf4j.impl.StaticLoggerBinder; import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggerConfiguration; -import org.springframework.boot.logging.LoggerConfigurationComparator; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.Slf4JLoggingSystem; @@ -59,9 +58,6 @@ import org.springframework.util.StringUtils; */ public class LogbackLoggingSystem extends Slf4JLoggingSystem { - private static final LoggerConfigurationComparator COMPARATOR = new LoggerConfigurationComparator( - Logger.ROOT_LOGGER_NAME); - private static final String CONFIGURATION_FILE_PROPERTY = "logback.configurationFile"; private static final LogLevels LEVELS = new LogLevels(); @@ -219,7 +215,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { for (ch.qos.logback.classic.Logger logger : getLoggerContext().getLoggerList()) { result.add(getLoggerConfiguration(logger)); } - Collections.sort(result, COMPARATOR); + Collections.sort(result, CONFIGURATION_COMPARATOR); return result; } @@ -236,7 +232,11 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { LogLevel level = LEVELS.convertNativeToSystem(logger.getLevel()); LogLevel effectiveLevel = LEVELS .convertNativeToSystem(logger.getEffectiveLevel()); - return new LoggerConfiguration(logger.getName(), level, effectiveLevel); + String name = logger.getName(); + if (!StringUtils.hasLength(name) || Logger.ROOT_LOGGER_NAME.equals(name)) { + name = ROOT_LOGGER_NAME; + } + return new LoggerConfiguration(name, level, effectiveLevel); } @Override @@ -259,7 +259,9 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { private ch.qos.logback.classic.Logger getLogger(String name) { LoggerContext factory = getLoggerContext(); - name = (StringUtils.isEmpty(name) ? Logger.ROOT_LOGGER_NAME : name); + if (StringUtils.isEmpty(name) || ROOT_LOGGER_NAME.equals(name)) { + name = Logger.ROOT_LOGGER_NAME; + } return factory.getLogger(name); } diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java index bc8a53b83f..c6010bc8e9 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/java/JavaLoggingSystemTests.java @@ -33,6 +33,7 @@ import org.junit.Test; import org.springframework.boot.logging.AbstractLoggingSystemTests; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggerConfiguration; +import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -175,7 +176,7 @@ public class JavaLoggingSystemTests extends AbstractLoggingSystemTests { List configurations = this.loggingSystem .getLoggerConfigurations(); assertThat(configurations).isNotEmpty(); - assertThat(configurations.get(0).getName()).isEmpty(); + assertThat(configurations.get(0).getName()).isEqualTo(LoggingSystem.ROOT_LOGGER_NAME); } @Test diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index b2abdb969f..06743df6c3 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -40,6 +40,7 @@ import org.junit.Test; import org.springframework.boot.logging.AbstractLoggingSystemTests; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggerConfiguration; +import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.boot.testutil.Matched; import org.springframework.util.FileCopyUtils; @@ -148,7 +149,8 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { List configurations = this.loggingSystem .getLoggerConfigurations(); assertThat(configurations).isNotEmpty(); - assertThat(configurations.get(0).getName()).isEmpty(); + assertThat(configurations.get(0).getName()) + .isEqualTo(LoggingSystem.ROOT_LOGGER_NAME); } @Test diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index b7f2e117b0..730a2efb2b 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -43,6 +43,7 @@ import org.springframework.boot.logging.LogFile; import org.springframework.boot.logging.LogLevel; import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggingInitializationContext; +import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.testutil.InternalOutputCapture; import org.springframework.boot.testutil.Matched; import org.springframework.mock.env.MockEnvironment; @@ -190,7 +191,7 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { .getLoggerConfigurations(); assertThat(configurations).isNotEmpty(); assertThat(configurations.get(0).getName()) - .isEqualTo(org.slf4j.Logger.ROOT_LOGGER_NAME); + .isEqualTo(LoggingSystem.ROOT_LOGGER_NAME); } @Test