diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java index 708e6334fc..4c39ebb2c9 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java @@ -16,8 +16,6 @@ package org.springframework.boot.logging.log4j2; -import java.net.URI; - import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.ConfigurationFactory; @@ -26,42 +24,38 @@ 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 prevents logger warnings from being - * printed when the application first starts. + * printed when the application first starts. This factory is ordered last and is + * triggered by a {@code log4j2.springboot} classpath resource (which is bundled in this + * jar). If the {@link Log4J2LoggingSystem} is active, a {@link DefaultConfiguration} is + * returned with the expectation that the system will later re-initialize Log4J2 with the + * correct configuration file. * * @author Phillip Webb * @since 1.5.0 */ @Plugin(name = "SpringBootConfigurationFactory", category = ConfigurationFactory.CATEGORY) -@Order(4) // Order behind XmlConfigurationFactory +@Order(0) public class SpringBootConfigurationFactory extends ConfigurationFactory { - private static final String[] ALL_TYPES = { "*" }; + private static final String[] TYPES = { ".springboot" }; @Override protected String[] getSupportedTypes() { - return ALL_TYPES; - } - - @Override - public Configuration getConfiguration(LoggerContext loggerContext, String name, - URI configLocation) { - if (configLocation == null) { - return new DefaultConfiguration(); - } - return null; - } - - @Override - public Configuration getConfiguration(LoggerContext loggerContext, String name, - URI configLocation, ClassLoader loader) { - return null; + return TYPES; } @Override public Configuration getConfiguration(LoggerContext loggerContext, ConfigurationSource source) { + if (source != null && source != ConfigurationSource.NULL_SOURCE) { + if (LoggingSystem.get(loggerContext.getClass().getClassLoader()) != null) { + return new DefaultConfiguration(); + } + } return null; } diff --git a/spring-boot/src/main/resources/log4j2.springboot b/spring-boot/src/main/resources/log4j2.springboot new file mode 100644 index 0000000000..0513a5fe82 --- /dev/null +++ b/spring-boot/src/main/resources/log4j2.springboot @@ -0,0 +1 @@ +See SpringBootConfigurationFactory