Commit 0dc2375e authored by Stephane Nicoll's avatar Stephane Nicoll

Merge pull request #14565 from izeye:deferred-log

* pr/14565:
  Add log messages to lines only when the destination isn't set
parents 0a310256 3dc78d19
...@@ -142,7 +142,9 @@ public class DeferredLog implements Log { ...@@ -142,7 +142,9 @@ public class DeferredLog implements Log {
if (this.destination != null) { if (this.destination != null) {
logTo(this.destination, level, message, t); logTo(this.destination, level, message, t);
} }
this.lines.add(new Line(level, message, t)); else {
this.lines.add(new Line(level, message, t));
}
} }
} }
......
...@@ -16,9 +16,13 @@ ...@@ -16,9 +16,13 @@
package org.springframework.boot.logging; package org.springframework.boot.logging;
import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
...@@ -169,9 +173,21 @@ public class DeferredLogTests { ...@@ -169,9 +173,21 @@ public class DeferredLogTests {
@Test @Test
public void switchTo() { public void switchTo() {
DirectFieldAccessor deferredLogFieldAccessor = new DirectFieldAccessor(
this.deferredLog);
List<String> lines = (List<String>) deferredLogFieldAccessor
.getPropertyValue("lines");
assertThat(lines).isEmpty();
this.deferredLog.error(this.message, this.throwable); this.deferredLog.error(this.message, this.throwable);
assertThat(lines).hasSize(1);
this.deferredLog.switchTo(this.log); this.deferredLog.switchTo(this.log);
assertThat(lines).isEmpty();
this.deferredLog.info("Message2"); this.deferredLog.info("Message2");
assertThat(lines).isEmpty();
verify(this.log).error(this.message, this.throwable); verify(this.log).error(this.message, this.throwable);
verify(this.log).info("Message2", null); verify(this.log).info("Message2", null);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment