Browse Source

Return null when getting a logback logger that does not exist

Closes gh-21292
pull/21361/head
Madhura Bhave 6 years ago
parent
commit
7634901fb3
  1. 21
      spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java
  2. 8
      spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

21
spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java

@ -234,7 +234,16 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { @@ -234,7 +234,16 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
@Override
public LoggerConfiguration getLoggerConfiguration(String loggerName) {
return getLoggerConfiguration(getLogger(loggerName));
String name = getLoggerName(loggerName);
LoggerContext loggerContext = getLoggerContext();
return getLoggerConfiguration(loggerContext.exists(name));
}
private String getLoggerName(String name) {
if (!StringUtils.hasLength(name) || Logger.ROOT_LOGGER_NAME.equals(name)) {
return ROOT_LOGGER_NAME;
}
return name;
}
private LoggerConfiguration getLoggerConfiguration(ch.qos.logback.classic.Logger logger) {
@ -243,10 +252,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { @@ -243,10 +252,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
}
LogLevel level = LEVELS.convertNativeToSystem(logger.getLevel());
LogLevel effectiveLevel = LEVELS.convertNativeToSystem(logger.getEffectiveLevel());
String name = logger.getName();
if (!StringUtils.hasLength(name) || Logger.ROOT_LOGGER_NAME.equals(name)) {
name = ROOT_LOGGER_NAME;
}
String name = getLoggerName(logger.getName());
return new LoggerConfiguration(name, level, effectiveLevel);
}
@ -270,10 +276,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { @@ -270,10 +276,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem {
private ch.qos.logback.classic.Logger getLogger(String name) {
LoggerContext factory = getLoggerContext();
if (StringUtils.isEmpty(name) || ROOT_LOGGER_NAME.equals(name)) {
name = Logger.ROOT_LOGGER_NAME;
}
return factory.getLogger(name);
return factory.getLogger(getLoggerName(name));
}
private LoggerContext getLoggerContext() {

8
spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

@ -206,6 +206,14 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { @@ -206,6 +206,14 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
.isEqualTo(new LoggerConfiguration(getClass().getName(), LogLevel.DEBUG, LogLevel.DEBUG));
}
@Test
void getLoggingConfigurationForLoggerThatDoesNotExistShouldReturnNull() {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null, null);
LoggerConfiguration configuration = this.loggingSystem.getLoggerConfiguration("doesnotexist");
assertThat(configuration).isNull();
}
@Test
void getLoggingConfigurationForALL() {
this.loggingSystem.beforeInitialize();

Loading…
Cancel
Save