From 42fbaafa6674b0a34479587aa8175303ba7c20a2 Mon Sep 17 00:00:00 2001 From: syd Date: Sat, 1 Mar 2025 01:04:13 +0800 Subject: [PATCH 1/4] Fix shutdownDisable not taking effect(#2614) --- .../logging/log4j/core/ShutdownDisabledTest.java | 10 +++++++++- log4j-core-test/src/test/resources/log4j-test3.xml | 2 +- .../org/apache/logging/log4j/core/LoggerContext.java | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java index 889d702fec0..851aee25f14 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java @@ -17,16 +17,24 @@ package org.apache.logging.log4j.core; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import java.lang.reflect.Field; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.test.junit.LoggerContextSource; +import org.apache.logging.log4j.core.util.ReflectionUtil; +import org.apache.logging.log4j.test.junit.SetTestProperty; import org.junit.jupiter.api.Test; +@SetTestProperty(key = "log4j2.is.webapp", value = "false") @LoggerContextSource("log4j-test3.xml") class ShutdownDisabledTest { @Test - void testShutdownFlag(final Configuration config) { + void testShutdownFlag(final Configuration config, final LoggerContext ctx) throws NoSuchFieldException { + Field shutdownCallback = LoggerContext.class.getDeclaredField("shutdownCallback"); + Object fieldValue = ReflectionUtil.getFieldValue(shutdownCallback, ctx); assertFalse(config.isShutdownHookEnabled(), "Shutdown hook is enabled"); + assertNull(fieldValue, "Shutdown callback is null"); } } diff --git a/log4j-core-test/src/test/resources/log4j-test3.xml b/log4j-core-test/src/test/resources/log4j-test3.xml index cef870f34b1..ca2287d5828 100644 --- a/log4j-core-test/src/test/resources/log4j-test3.xml +++ b/log4j-core-test/src/test/resources/log4j-test3.xml @@ -15,7 +15,7 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java index 5d486c7649d..77f10593f8c 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java @@ -321,6 +321,8 @@ public void start(final Configuration config) { if (configLock.tryLock()) { try { if (this.isInitialized() || this.isStopped()) { + this.setStarting(); + setConfiguration(config); if (this.configuration.isShutdownHookEnabled()) { setUpShutdownHook(); } @@ -330,7 +332,6 @@ public void start(final Configuration config) { configLock.unlock(); } } - setConfiguration(config); LOGGER.info("{}[name={}] started with configuration {}.", getClass().getSimpleName(), getName(), config); } From fc1fb4776e4a3f6d0d5ef75a797991b23676397b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Fri, 14 Mar 2025 10:02:56 +0100 Subject: [PATCH 2/4] Fix the `log4j2.isWebapp` property naming Co-authored-by: Piotr P. Karwasz --- .../org/apache/logging/log4j/core/ShutdownDisabledTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java index 851aee25f14..fd7dbf2607b 100644 --- a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java @@ -26,7 +26,7 @@ import org.apache.logging.log4j.test.junit.SetTestProperty; import org.junit.jupiter.api.Test; -@SetTestProperty(key = "log4j2.is.webapp", value = "false") +@SetTestProperty(key = "log4j2.isWebapp", value = "false") @LoggerContextSource("log4j-test3.xml") class ShutdownDisabledTest { From 0be6b196796c9189ea5e28792fe8de991832e12d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Fri, 14 Mar 2025 10:03:52 +0100 Subject: [PATCH 3/4] Revert the `status` change --- log4j-core-test/src/test/resources/log4j-test3.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/log4j-core-test/src/test/resources/log4j-test3.xml b/log4j-core-test/src/test/resources/log4j-test3.xml index ca2287d5828..cef870f34b1 100644 --- a/log4j-core-test/src/test/resources/log4j-test3.xml +++ b/log4j-core-test/src/test/resources/log4j-test3.xml @@ -15,7 +15,7 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + From ecf8b88ffb6d7c4f95a913eabb65b3f402d9f479 Mon Sep 17 00:00:00 2001 From: syd Date: Sat, 15 Mar 2025 01:14:40 +0800 Subject: [PATCH 4/4] Fix shutdownDisable not taking effect (#2614) --- .../java/org/apache/logging/log4j/core/LoggerContext.java | 4 ++-- .../log4j/spring/boot/Log4j2SpringBootLoggingSystem.java | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java index 77f10593f8c..bf2f77383c0 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java @@ -321,8 +321,8 @@ public void start(final Configuration config) { if (configLock.tryLock()) { try { if (this.isInitialized() || this.isStopped()) { - this.setStarting(); - setConfiguration(config); + setStarting(); + reconfigure(config); if (this.configuration.isShutdownHookEnabled()) { setUpShutdownHook(); } diff --git a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystem.java b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystem.java index 65bc1163e64..4db51b61653 100644 --- a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystem.java +++ b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/Log4j2SpringBootLoggingSystem.java @@ -158,7 +158,7 @@ protected void loadConfiguration(final String location, final LogFile logFile, f final URL url = ResourceUtils.getURL(location); final ConfigurationSource source = getConfigurationSource(url); if (source != null) { - ctx.start(ConfigurationFactory.getInstance().getConfiguration(ctx, source)); + ctx.reconfigure(ConfigurationFactory.getInstance().getConfiguration(ctx, source)); } } else { final List configs = new ArrayList<>(); @@ -189,9 +189,9 @@ protected void loadConfiguration(final String location, final LogFile logFile, f first = false; } if (configs.size() > 1) { - ctx.start(new CompositeConfiguration(configs)); + ctx.reconfigure(new CompositeConfiguration(configs)); } else { - ctx.start(configs.get(0)); + ctx.reconfigure(configs.get(0)); } } } catch (Exception ex) {