Commit 8854526c authored by Phillip Webb's avatar Phillip Webb

Refine 'No log4j2 configuration file found' hiding

Refine commit 1e9e1b04 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
parent 7c25ccb0
...@@ -16,8 +16,6 @@ ...@@ -16,8 +16,6 @@
package org.springframework.boot.logging.log4j2; package org.springframework.boot.logging.log4j2;
import java.net.URI;
import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.config.ConfigurationFactory;
...@@ -26,42 +24,38 @@ import org.apache.logging.log4j.core.config.DefaultConfiguration; ...@@ -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.Order;
import org.apache.logging.log4j.core.config.plugins.Plugin; 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 * 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 * @author Phillip Webb
* @since 1.5.0 * @since 1.5.0
*/ */
@Plugin(name = "SpringBootConfigurationFactory", category = ConfigurationFactory.CATEGORY) @Plugin(name = "SpringBootConfigurationFactory", category = ConfigurationFactory.CATEGORY)
@Order(4) // Order behind XmlConfigurationFactory @Order(0)
public class SpringBootConfigurationFactory extends ConfigurationFactory { public class SpringBootConfigurationFactory extends ConfigurationFactory {
private static final String[] ALL_TYPES = { "*" }; private static final String[] TYPES = { ".springboot" };
@Override @Override
protected String[] getSupportedTypes() { protected String[] getSupportedTypes() {
return ALL_TYPES; return TYPES;
} }
@Override @Override
public Configuration getConfiguration(LoggerContext loggerContext, String name, public Configuration getConfiguration(LoggerContext loggerContext,
URI configLocation) { ConfigurationSource source) {
if (configLocation == null) { if (source != null && source != ConfigurationSource.NULL_SOURCE) {
if (LoggingSystem.get(loggerContext.getClass().getClassLoader()) != null) {
return new DefaultConfiguration(); return new DefaultConfiguration();
} }
return null;
} }
@Override
public Configuration getConfiguration(LoggerContext loggerContext, String name,
URI configLocation, ClassLoader loader) {
return null;
}
@Override
public Configuration getConfiguration(LoggerContext loggerContext,
ConfigurationSource source) {
return null; return null;
} }
......
See SpringBootConfigurationFactory
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment