From 504de8a7c82145bbd36019d511c138687588f23a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 10 Nov 2014 15:05:23 +0000 Subject: [PATCH] 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 --- .../logging/log4j2/Log4J2LoggingSystemTests.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index bf9305b1e92..c749b30d78b 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -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 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();