From 9cf13004d75a0cfb90e630356119f63e2795537a Mon Sep 17 00:00:00 2001 From: Fabrice Bibonne Date: Thu, 26 Feb 2026 06:05:39 +0100 Subject: [PATCH 1/2] Allow Log4j2 logging system to be forced by system property Signed-off-by: Fabrice Bibonne See gh-49343 --- .../logging/log4j2/Log4J2LoggingSystem.java | 19 ++++++++++++------- .../boot/logging/LoggingSystemTests.java | 12 ++++++++++++ .../log4j2/Log4J2LoggingSystemTests.java | 1 + .../log4j2/TestLog4J2LoggingSystem.java | 4 ++-- 4 files changed, 27 insertions(+), 9 deletions(-) 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 cea74711abc..cb7a71ce397 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 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; 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 cc491eaa349..def94bc4c3e 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 @@ 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 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 88025e009d9..ecb14ddbd98 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 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { 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 ca937aa5879..fe522378a1d 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(); } From 0dd7f6d78d525ea097a20c33e08ab6d3af82689e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 3 Mar 2026 11:15:51 +0000 Subject: [PATCH 2/2] Polish "Allow Log4j2 logging system to be forced by system property" See gh-49343 --- .../boot/logging/log4j2/Log4J2LoggingSystem.java | 7 +++---- .../springframework/boot/logging/LoggingSystemTests.java | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) 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 cb7a71ce397..e093a4ce6a3 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,14 +121,13 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { /** * Create a new {@link Log4J2LoggingSystem} instance. * @param classLoader the class loader to use. - * @throws IllegalArgumentException if the loggerContext instantiated internally is - * not of type org.apache.logging.log4j.core.LoggerContext + * @throws IllegalArgumentException if the logger context is not a + * {@link LoggerContext}. */ Log4J2LoggingSystem(ClassLoader classLoader) { super(classLoader); 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"); + Assert.isInstanceOf(LoggerContext.class, spiLoggerContext); this.loggerContext = (LoggerContext) spiLoggerContext; } 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 def94bc4c3e..159ba949d3e 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 @@ -72,7 +72,7 @@ class LoggingSystemTests { } @Test - void julj2CanBeForcedUsingSystemProperty() { + void julCanBeForcedUsingSystemProperty() { System.setProperty(LoggingSystem.SYSTEM_PROPERTY, JavaLoggingSystem.class.getName()); assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(JavaLoggingSystem.class); }