Flush OutputStream-based appenders in Log4J 2 Tests

CI has been failing intermittently with failures in
Log4J2LoggingSystemTests. A possible cause of the failures is that
log entries are being buffered in an output stream. This may cause
an expected log entry to be absent (the entry is buffered) or an
unexpected entry to be present (the previously buffered entry has
now been flushed).

This commit attempts to address the test failures by ensuring that
all OutputStream-based appenders are flushed at the end of every test.

Closes gh-1864
pull/1862/merge
Andy Wilkinson 10 years ago
parent 9259564307
commit 504de8a7c8

@ -17,9 +17,13 @@
package org.springframework.boot.logging.log4j2;
import java.io.File;
import java.util.Map.Entry;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.appender.AbstractOutputStreamAppender;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
@ -55,6 +59,17 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
this.logger = LogManager.getLogger(getClass());
}
@After
public void flushAllOutputStreamAppenders() {
for (Entry<String, Appender> entry : ((org.apache.logging.log4j.core.Logger) this.logger)
.getAppenders().entrySet()) {
Appender appender = entry.getValue();
if (appender instanceof AbstractOutputStreamAppender) {
((AbstractOutputStreamAppender<?>) appender).getManager().flush();
}
}
}
@Test
public void noFile() throws Exception {
this.loggingSystem.beforeInitialize();

Loading…
Cancel
Save