Skip to content

Commit 42fbaaf

Browse files
committed
Fix shutdownDisable not taking effect(#2614)
1 parent 1da1d7e commit 42fbaaf

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@
1717
package org.apache.logging.log4j.core;
1818

1919
import static org.junit.jupiter.api.Assertions.assertFalse;
20+
import static org.junit.jupiter.api.Assertions.assertNull;
2021

22+
import java.lang.reflect.Field;
2123
import org.apache.logging.log4j.core.config.Configuration;
2224
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
25+
import org.apache.logging.log4j.core.util.ReflectionUtil;
26+
import org.apache.logging.log4j.test.junit.SetTestProperty;
2327
import org.junit.jupiter.api.Test;
2428

29+
@SetTestProperty(key = "log4j2.is.webapp", value = "false")
2530
@LoggerContextSource("log4j-test3.xml")
2631
class ShutdownDisabledTest {
2732

2833
@Test
29-
void testShutdownFlag(final Configuration config) {
34+
void testShutdownFlag(final Configuration config, final LoggerContext ctx) throws NoSuchFieldException {
35+
Field shutdownCallback = LoggerContext.class.getDeclaredField("shutdownCallback");
36+
Object fieldValue = ReflectionUtil.getFieldValue(shutdownCallback, ctx);
3037
assertFalse(config.isShutdownHookEnabled(), "Shutdown hook is enabled");
38+
assertNull(fieldValue, "Shutdown callback is null");
3139
}
3240
}

log4j-core-test/src/test/resources/log4j-test3.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
~ See the License for the specific language governing permissions and
1616
~ limitations under the License.
1717
-->
18-
<Configuration status="OFF" name="XMLConfigTest" monitorInterval="5" shutdownHook="disable">
18+
<Configuration status="DEBUG" name="XMLConfigTest" monitorInterval="5" shutdownHook="disable">
1919
<Appenders>
2020
<Console name="STDOUT">
2121
<PatternLayout pattern="%m MDC%X%n"/>

log4j-core/src/main/java/org/apache/logging/log4j/core/LoggerContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ public void start(final Configuration config) {
321321
if (configLock.tryLock()) {
322322
try {
323323
if (this.isInitialized() || this.isStopped()) {
324+
this.setStarting();
325+
setConfiguration(config);
324326
if (this.configuration.isShutdownHookEnabled()) {
325327
setUpShutdownHook();
326328
}
@@ -330,7 +332,6 @@ public void start(final Configuration config) {
330332
configLock.unlock();
331333
}
332334
}
333-
setConfiguration(config);
334335
LOGGER.info("{}[name={}] started with configuration {}.", getClass().getSimpleName(), getName(), config);
335336
}
336337

0 commit comments

Comments
 (0)