diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/DeferredLog.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/DeferredLog.java index 2d86075b71..5ac65e0f1d 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/DeferredLog.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/DeferredLog.java @@ -142,7 +142,9 @@ public class DeferredLog implements Log { if (this.destination != null) { logTo(this.destination, level, message, t); } - this.lines.add(new Line(level, message, t)); + else { + this.lines.add(new Line(level, message, t)); + } } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/DeferredLogTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/DeferredLogTests.java index 9b5adfbef4..c807c3ba72 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/DeferredLogTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/DeferredLogTests.java @@ -16,9 +16,13 @@ package org.springframework.boot.logging; +import java.util.List; + import org.apache.commons.logging.Log; import org.junit.Test; +import org.springframework.beans.DirectFieldAccessor; + import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -169,9 +173,21 @@ public class DeferredLogTests { @Test public void switchTo() { + DirectFieldAccessor deferredLogFieldAccessor = new DirectFieldAccessor( + this.deferredLog); + List lines = (List) deferredLogFieldAccessor + .getPropertyValue("lines"); + assertThat(lines).isEmpty(); + this.deferredLog.error(this.message, this.throwable); + assertThat(lines).hasSize(1); + this.deferredLog.switchTo(this.log); + assertThat(lines).isEmpty(); + this.deferredLog.info("Message2"); + assertThat(lines).isEmpty(); + verify(this.log).error(this.message, this.throwable); verify(this.log).info("Message2", null); }