From 0ce3e7ec6b1bc20c8ba268ae75536b83343aac3e Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 17 Nov 2020 17:07:28 -0800 Subject: [PATCH] Protect against NPE caused by recursive calls Update `SpringBootConfigurationFactory` so that it no longer attempts to get a `LoggingSystem`. The recent `LoggingSystem` update means makes use of the `SpringFactoriesLoader` class to load candidate logging systems. Unfortunately, the `SpringFactoriesLoader` class creates a `Logger` which (when using Log4J2) causes `SpringBootConfigurationFactory` to run. Calling `LoggingSystem.get` from `SpringBootConfigurationFactory` results in a recursive call to `SpringFactoriesLoader` which hasn't yet been fully initialized. We then see an NPE caused by a `null` `cache`. This update removes the call to `LoggingSystem.get` with the assumption that it would never return `null` anyway. Fixes gh-24163 --- .../logging/log4j2/SpringBootConfigurationFactory.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java index 824b59ffe1..9f7fcb8f91 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java @@ -24,8 +24,6 @@ import org.apache.logging.log4j.core.config.DefaultConfiguration; import org.apache.logging.log4j.core.config.Order; import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.springframework.boot.logging.LoggingSystem; - /** * Spring Boot {@link ConfigurationFactory} that customizes Log4J2's default configuration * to: @@ -56,11 +54,10 @@ public class SpringBootConfigurationFactory extends ConfigurationFactory { @Override public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { - if (source != null && source != ConfigurationSource.NULL_SOURCE - && LoggingSystem.get(loggerContext.getClass().getClassLoader()) != null) { - return new SpringBootConfiguration(); + if (source == null || source == ConfigurationSource.NULL_SOURCE) { + return null; } - return null; + return new SpringBootConfiguration(); } private static final class SpringBootConfiguration extends DefaultConfiguration {