Skip to content

Commit 4fa55de

Browse files
committed
fix(tests): adapt to Log4J2LoggingSystem refactor
This commit updates the test suite to align with the recent `Log4J2LoggingSystem` changes: * Each test now uses its own `LoggerContext`, making manual reset of previous state unnecessary. * Removes tests for `getStandardConfigLocation()` and `getSpringConfigLocation()`, as these methods are now deprecated no-ops and no longer attempt to infer configuration files based on classpath contents. * Removes a test for `UrlConfigurationFactory`, which is no longer in use. Signed-off-by: Piotr P. Karwasz <piotr@github.copernik.eu>
1 parent cf2dcfb commit 4fa55de

File tree

4 files changed

+96
-213
lines changed

4 files changed

+96
-213
lines changed

core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.apache.logging.log4j.core.filter.DenyAllFilter;
4141
import org.apache.logging.log4j.core.util.NameUtil;
4242
import org.apache.logging.log4j.jul.Log4jBridgeHandler;
43-
import org.apache.logging.log4j.spi.LoggerContextFactory;
4443
import org.apache.logging.log4j.status.StatusConsoleListener;
4544
import org.apache.logging.log4j.status.StatusLogger;
4645
import org.apache.logging.log4j.util.PropertiesUtil;
@@ -110,9 +109,28 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
110109

111110
private final LoggerContext loggerContext;
112111

113-
private Log4J2LoggingSystem(ClassLoader classLoader, org.apache.logging.log4j.spi.LoggerContext loggerContext) {
112+
/**
113+
* Create a new {@link Log4J2LoggingSystem} instance.
114+
* @param classLoader the class loader to use.
115+
* @param loggerContext the {@link LoggerContext} to use.
116+
*/
117+
Log4J2LoggingSystem(ClassLoader classLoader, LoggerContext loggerContext) {
114118
super(classLoader);
115-
this.loggerContext = (LoggerContext) loggerContext;
119+
this.loggerContext = loggerContext;
120+
}
121+
122+
/**
123+
* Create a new {@link Log4J2LoggingSystem} instance.
124+
* @param classLoader the class loader to use
125+
* @return a new {@link Log4J2LoggingSystem} instance
126+
* @throws IllegalStateException if Log4j Core is not the active Log4j API provider.
127+
*/
128+
private static Log4J2LoggingSystem createLoggingSystem(ClassLoader classLoader) {
129+
org.apache.logging.log4j.spi.LoggerContext loggerContext = LogManager.getContext(classLoader, false);
130+
if (loggerContext instanceof LoggerContext) {
131+
return new Log4J2LoggingSystem(classLoader, (LoggerContext) loggerContext);
132+
}
133+
throw new IllegalStateException("Log4j Core is not the active Log4j API provider");
116134
}
117135

118136
/**
@@ -136,7 +154,8 @@ protected String[] getStandardConfigLocations() {
136154
ConfigurationFactory configurationFactory = ConfigurationFactory.getInstance();
137155
Configuration springConfiguration = configurationFactory.getConfiguration(getLoggerContext(), "-spring", null,
138156
getClassLoader());
139-
return getConfigLocation(springConfiguration);
157+
String configLocation = getConfigLocation(springConfiguration);
158+
return (configLocation != null && configLocation.contains("-spring")) ? configLocation : null;
140159
}
141160

142161
private @Nullable String getConfigLocation(Configuration configuration) {
@@ -460,7 +479,7 @@ public void cleanUp() {
460479
return configuration.getLoggers().get(name);
461480
}
462481

463-
private LoggerContext getLoggerContext() {
482+
LoggerContext getLoggerContext() {
464483
return this.loggerContext;
465484
}
466485

@@ -496,20 +515,22 @@ protected String getDefaultLogCorrelationPattern() {
496515
* {@link LoggingSystemFactory} that returns {@link Log4J2LoggingSystem} if possible.
497516
*/
498517
@Order(0)
499-
public static class Factory extends LogManager implements LoggingSystemFactory {
500-
501-
private static final String FQCN = Factory.class.getName();
518+
public static class Factory implements LoggingSystemFactory {
502519

503520
private static final String LOG4J_CORE_CONTEXT_FACTORY = "org.apache.logging.log4j.core.impl.Log4jContextFactory";
504521

522+
private static final boolean PRESENT = ClassUtils.isPresent(LOG4J_CORE_CONTEXT_FACTORY,
523+
Factory.class.getClassLoader());
524+
505525
@Override
506526
public @Nullable LoggingSystem getLoggingSystem(ClassLoader classLoader) {
507-
LoggerContextFactory contextFactory = getFactory();
508-
// At the same time, we check that Log4j Core is present and that it is the
509-
// active
510-
// implementation.
511-
if (LOG4J_CORE_CONTEXT_FACTORY.equals(contextFactory.getClass().getName())) {
512-
return new Log4J2LoggingSystem(classLoader, contextFactory.getContext(FQCN, classLoader, null, false));
527+
if (PRESENT) {
528+
try {
529+
return createLoggingSystem(classLoader);
530+
}
531+
catch (IllegalStateException ex) {
532+
// Log4j Core is not the active Log4j API provider
533+
}
513534
}
514535
return null;
515536
}

0 commit comments

Comments
 (0)