Skip to content

Commit 7634901

Browse files
committed
Return null when getting a logback logger that does not exist
Closes gh-21292
1 parent f23d050 commit 7634901

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

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

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,16 @@ public List<LoggerConfiguration> getLoggerConfigurations() {
234234

235235
@Override
236236
public LoggerConfiguration getLoggerConfiguration(String loggerName) {
237-
return getLoggerConfiguration(getLogger(loggerName));
237+
String name = getLoggerName(loggerName);
238+
LoggerContext loggerContext = getLoggerContext();
239+
return getLoggerConfiguration(loggerContext.exists(name));
240+
}
241+
242+
private String getLoggerName(String name) {
243+
if (!StringUtils.hasLength(name) || Logger.ROOT_LOGGER_NAME.equals(name)) {
244+
return ROOT_LOGGER_NAME;
245+
}
246+
return name;
238247
}
239248

240249
private LoggerConfiguration getLoggerConfiguration(ch.qos.logback.classic.Logger logger) {
@@ -243,10 +252,7 @@ private LoggerConfiguration getLoggerConfiguration(ch.qos.logback.classic.Logger
243252
}
244253
LogLevel level = LEVELS.convertNativeToSystem(logger.getLevel());
245254
LogLevel effectiveLevel = LEVELS.convertNativeToSystem(logger.getEffectiveLevel());
246-
String name = logger.getName();
247-
if (!StringUtils.hasLength(name) || Logger.ROOT_LOGGER_NAME.equals(name)) {
248-
name = ROOT_LOGGER_NAME;
249-
}
255+
String name = getLoggerName(logger.getName());
250256
return new LoggerConfiguration(name, level, effectiveLevel);
251257
}
252258

@@ -270,10 +276,7 @@ public Runnable getShutdownHandler() {
270276

271277
private ch.qos.logback.classic.Logger getLogger(String name) {
272278
LoggerContext factory = getLoggerContext();
273-
if (StringUtils.isEmpty(name) || ROOT_LOGGER_NAME.equals(name)) {
274-
name = Logger.ROOT_LOGGER_NAME;
275-
}
276-
return factory.getLogger(name);
279+
return factory.getLogger(getLoggerName(name));
277280
}
278281

279282
private LoggerContext getLoggerContext() {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ void getLoggingConfiguration() {
206206
.isEqualTo(new LoggerConfiguration(getClass().getName(), LogLevel.DEBUG, LogLevel.DEBUG));
207207
}
208208

209+
@Test
210+
void getLoggingConfigurationForLoggerThatDoesNotExistShouldReturnNull() {
211+
this.loggingSystem.beforeInitialize();
212+
this.loggingSystem.initialize(this.initializationContext, null, null);
213+
LoggerConfiguration configuration = this.loggingSystem.getLoggerConfiguration("doesnotexist");
214+
assertThat(configuration).isNull();
215+
}
216+
209217
@Test
210218
void getLoggingConfigurationForALL() {
211219
this.loggingSystem.beforeInitialize();

0 commit comments

Comments
 (0)