From 8854526c5e0364ea543ce3507f8596207edb5d46 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 29 Dec 2016 16:07:07 -0800 Subject: [PATCH] Refine 'No log4j2 configuration file found' hiding Refine commit 1e9e1b04d0 since it unfortunately had the side-effect of disabling logging when a valid `log4j2.xml` file was found. The updated code uses a similar technique but instead of accepting '*' as a file type it now looks for `.springboot` (which it finds because we ship a `log4j2.springboot` file in out JAR. Rather than exiting Log4J2's `ConfigurationFactory.Factory` `getConfiguration` method early, our `SpringBootConfigurationFactory` is now only triggered on the last call to `getConfiguration`. Closes gh-4809 --- .../SpringBootConfigurationFactory.java | 36 ++++++++----------- .../src/main/resources/log4j2.springboot | 1 + 2 files changed, 16 insertions(+), 21 deletions(-) create mode 100644 spring-boot/src/main/resources/log4j2.springboot 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 708e6334fc3..4c39ebb2c98 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 00000000000..0513a5fe82c --- /dev/null +++ b/spring-boot/src/main/resources/log4j2.springboot @@ -0,0 +1 @@ +See SpringBootConfigurationFactory