Skip to content

Commit 5ed645d

Browse files
committed
Avoid logging config leakage in log4j unit test.
1 parent c6478be commit 5ed645d

File tree

1 file changed

+11
-3
lines changed
  • powertools-logging/powertools-logging-log4j/src/test/java/software/amazon/lambda/powertools/logging/log4j/internal

1 file changed

+11
-3
lines changed

powertools-logging/powertools-logging-log4j/src/test/java/software/amazon/lambda/powertools/logging/log4j/internal/Log4jLoggingManagerTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@
1818
import org.apache.logging.log4j.core.config.Configuration;
1919
import org.apache.logging.log4j.core.config.ConfigurationFactory;
2020
import org.apache.logging.log4j.core.config.ConfigurationSource;
21+
import org.apache.logging.log4j.core.config.Configurator;
2122
import org.apache.logging.log4j.core.config.xml.XmlConfigurationFactory;
2223
import org.junit.jupiter.api.AfterEach;
23-
import org.junit.jupiter.api.Order;
24+
import org.junit.jupiter.api.BeforeEach;
2425
import org.junit.jupiter.api.Test;
2526
import org.slf4j.Logger;
2627
import org.slf4j.LoggerFactory;
@@ -31,8 +32,13 @@ class Log4jLoggingManagerTest {
3132
private static final Logger LOG = LoggerFactory.getLogger(Log4jLoggingManagerTest.class);
3233
private static final Logger ROOT = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
3334

35+
@BeforeEach
36+
void setUp() {
37+
// Force reconfiguration from XML to ensure clean state
38+
Configurator.reconfigure();
39+
}
40+
3441
@Test
35-
@Order(1)
3642
void getLogLevel_shouldReturnConfiguredLogLevel() {
3743
// Given log4j2.xml in resources
3844

@@ -47,7 +53,6 @@ void getLogLevel_shouldReturnConfiguredLogLevel() {
4753
}
4854

4955
@Test
50-
@Order(2)
5156
void resetLogLevel() {
5257
// Given log4j2.xml in resources
5358

@@ -109,6 +114,9 @@ void shouldDetectMultipleBufferingAppendersRegardlessOfName() throws IOException
109114

110115
@AfterEach
111116
void cleanUp() throws IOException {
117+
// Reset to original configuration from XML
118+
Configurator.reconfigure();
119+
112120
try {
113121
FileChannel.open(Paths.get("target/logfile.json"), StandardOpenOption.WRITE).truncate(0).close();
114122
} catch (NoSuchFileException e) {

0 commit comments

Comments
 (0)