From bff66bef736c58bcdcf1d21d0dc287586a5a8267 Mon Sep 17 00:00:00 2001 From: syd Date: Sun, 23 Feb 2025 20:55:28 +0800 Subject: [PATCH 1/2] Fix shutdownDisable not taking effect --- .../logging/log4j/core/ShutdownDisabledTest.java | 15 ++++++++++++--- .../src/test/resources/log4j-test3.xml | 2 +- .../apache/logging/log4j/core/LoggerContext.java | 3 ++- 3 files changed, 15 insertions(+), 5 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..94ca1bdca5b 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 @@ -16,17 +16,26 @@ */ package org.apache.logging.log4j.core; -import static org.junit.jupiter.api.Assertions.assertFalse; - 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; +import java.lang.reflect.Field; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; + +@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 c2cbdcab0e27541751ab6eb8ada82a7581a132e9 Mon Sep 17 00:00:00 2001 From: syd Date: Sun, 23 Feb 2025 21:53:03 +0800 Subject: [PATCH 2/2] Fix shutdownDisable not taking effect --- .../apache/logging/log4j/core/ShutdownDisabledTest.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 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 94ca1bdca5b..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 @@ -16,17 +16,16 @@ */ 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; -import java.lang.reflect.Field; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNull; - @SetTestProperty(key = "log4j2.is.webapp", value = "false") @LoggerContextSource("log4j-test3.xml") class ShutdownDisabledTest {