diff --git a/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index cea74711abc3..cb7a71ce3976 100644 --- a/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/core/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -121,11 +121,15 @@ 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 loggerContext instantiated internally is + * not of type org.apache.logging.log4j.core.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, + "Log4j2LoggingSystem requires LoggerContext to be of type org.apache.logging.log4j.core.LoggerContext"); + this.loggerContext = (LoggerContext) spiLoggerContext; } @Override @@ -528,10 +532,11 @@ public static class Factory implements LoggingSystemFactory { @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; diff --git a/core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java b/core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java index cc491eaa3497..def94bc4c3ec 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/logging/LoggingSystemTests.java @@ -65,6 +65,18 @@ void loggingSystemCanBeDisabled() { 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 julj2CanBeForcedUsingSystemProperty() { + System.setProperty(LoggingSystem.SYSTEM_PROPERTY, JavaLoggingSystem.class.getName()); + assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(JavaLoggingSystem.class); + } + @Test void getLoggerConfigurationIsUnsupported() { assertThatExceptionOfType(UnsupportedOperationException.class) diff --git a/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index 88025e009d9d..ecb14ddbd980 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -116,6 +116,7 @@ void cleanUp() { this.loggingSystem.getConfiguration().stop(); this.loggingSystem.cleanUp(); PluginRegistry.getInstance().clear(); + LogManager.getFactory().removeContext(this.loggingSystem.getLoggerContext()); } @Test diff --git a/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/TestLog4J2LoggingSystem.java b/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/TestLog4J2LoggingSystem.java index ca937aa5879b..fe522378a1d6 100644 --- a/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/TestLog4J2LoggingSystem.java +++ b/core/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/TestLog4J2LoggingSystem.java @@ -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(); }