Merge branch '4.0.x'

Closes gh-49384
This commit is contained in:
Andy Wilkinson
2026-03-03 11:24:20 +00:00
4 changed files with 26 additions and 9 deletions
@@ -123,11 +123,14 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
/**
* Create a new {@link Log4J2LoggingSystem} instance.
* @param classLoader the class loader to use.
* @param loggerContext the {@link LoggerContext} to use.
* @throws IllegalArgumentException if the logger context is not a
* {@link LoggerContext}.
*/
Log4J2LoggingSystem(ClassLoader classLoader, LoggerContext loggerContext) {
Log4J2LoggingSystem(ClassLoader classLoader) {
super(classLoader);
this.loggerContext = loggerContext;
org.apache.logging.log4j.spi.LoggerContext spiLoggerContext = LogManager.getContext(classLoader, false);
Assert.isInstanceOf(LoggerContext.class, spiLoggerContext);
this.loggerContext = (LoggerContext) spiLoggerContext;
}
@Override
@@ -535,10 +538,11 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
@Override
public @Nullable LoggingSystem getLoggingSystem(ClassLoader classLoader) {
if (PRESENT) {
org.apache.logging.log4j.spi.LoggerContext spiLoggerContext = LogManager.getContext(classLoader, false);
Assert.state(spiLoggerContext instanceof LoggerContext, "");
if (spiLoggerContext instanceof LoggerContext coreLoggerContext) {
return new Log4J2LoggingSystem(classLoader, coreLoggerContext);
try {
return new Log4J2LoggingSystem(classLoader);
}
catch (IllegalStateException ex) {
// Continue
}
}
return null;
@@ -65,6 +65,18 @@ class LoggingSystemTests {
assertThat(loggingSystem).isInstanceOf(NoOpLoggingSystem.class);
}
@Test
void log4j2CanBeForcedUsingSystemProperty() {
System.setProperty(LoggingSystem.SYSTEM_PROPERTY, Log4J2LoggingSystem.class.getName());
assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(Log4J2LoggingSystem.class);
}
@Test
void julCanBeForcedUsingSystemProperty() {
System.setProperty(LoggingSystem.SYSTEM_PROPERTY, JavaLoggingSystem.class.getName());
assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(JavaLoggingSystem.class);
}
@Test
void getLoggerConfigurationIsUnsupported() {
assertThatExceptionOfType(UnsupportedOperationException.class)
@@ -125,6 +125,7 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
this.loggingSystem.getConfiguration().stop();
this.loggingSystem.cleanUp();
PluginRegistry.getInstance().clear();
LogManager.getFactory().removeContext(this.loggingSystem.getLoggerContext());
clearRollingPolicySystemProperties();
}
@@ -16,7 +16,6 @@
package org.springframework.boot.logging.log4j2;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.jspecify.annotations.Nullable;
@@ -26,7 +25,8 @@ class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
TestLog4J2LoggingSystem(String contextName) {
// Tests add resources to the thread context classloader
super(Thread.currentThread().getContextClassLoader(), new LoggerContext(contextName));
super(Thread.currentThread().getContextClassLoader());
getLoggerContext().setName(contextName);
getLoggerContext().start();
}