Skip to content

Commit c6478be

Browse files
committed
Isolate log4j context loading in unit tests.
1 parent cc863c4 commit c6478be

File tree

1 file changed

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

1 file changed

+31
-23
lines changed

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

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -72,31 +72,39 @@ void shouldDetectMultipleBufferingAppendersRegardlessOfName() throws IOException
7272
// may not be there in the first run
7373
}
7474

75-
ConfigurationFactory factory = new XmlConfigurationFactory();
76-
ConfigurationSource source = new ConfigurationSource(
77-
getClass().getResourceAsStream("/log4j2-multiple-buffering.xml"));
78-
Configuration config = factory.getConfiguration(null, source);
79-
8075
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
81-
ctx.setConfiguration(config);
82-
ctx.updateLoggers();
83-
84-
org.apache.logging.log4j.Logger logger = LogManager.getLogger("test.multiple.appenders");
85-
86-
// When - log messages and flush buffers
87-
logger.debug("Test message 1");
88-
logger.debug("Test message 2");
76+
Configuration originalConfig = ctx.getConfiguration();
8977

90-
Log4jLoggingManager manager = new Log4jLoggingManager();
91-
manager.flushBuffer();
92-
93-
// Then - both appenders should have flushed their buffers
94-
File logFile = new File("target/logfile.json");
95-
assertThat(logFile).exists();
96-
String content = contentOf(logFile);
97-
// Each message should appear twice (once from each BufferingAppender)
98-
assertThat(content.split("Test message 1", -1)).hasSize(3); // 2 occurrences = 3 parts
99-
assertThat(content.split("Test message 2", -1)).hasSize(3); // 2 occurrences = 3 parts
78+
try {
79+
ConfigurationFactory factory = new XmlConfigurationFactory();
80+
ConfigurationSource source = new ConfigurationSource(
81+
getClass().getResourceAsStream("/log4j2-multiple-buffering.xml"));
82+
Configuration config = factory.getConfiguration(null, source);
83+
84+
ctx.setConfiguration(config);
85+
ctx.updateLoggers();
86+
87+
org.apache.logging.log4j.Logger logger = LogManager.getLogger("test.multiple.appenders");
88+
89+
// When - log messages and flush buffers
90+
logger.debug("Test message 1");
91+
logger.debug("Test message 2");
92+
93+
Log4jLoggingManager manager = new Log4jLoggingManager();
94+
manager.flushBuffer();
95+
96+
// Then - both appenders should have flushed their buffers
97+
File logFile = new File("target/logfile.json");
98+
assertThat(logFile).exists();
99+
String content = contentOf(logFile);
100+
// Each message should appear twice (once from each BufferingAppender)
101+
assertThat(content.split("Test message 1", -1)).hasSize(3); // 2 occurrences = 3 parts
102+
assertThat(content.split("Test message 2", -1)).hasSize(3); // 2 occurrences = 3 parts
103+
} finally {
104+
// Restore original configuration to prevent test interference
105+
ctx.setConfiguration(originalConfig);
106+
ctx.updateLoggers();
107+
}
100108
}
101109

102110
@AfterEach

0 commit comments

Comments
 (0)