Merge pull request #14565 from izeye:deferred-log

* pr/14565:
  Add log messages to lines only when the destination isn't set
pull/14592/head
Stephane Nicoll 6 years ago
commit 0dc2375e2e

@ -142,9 +142,11 @@ 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);
} }
else {
this.lines.add(new Line(level, message, t)); this.lines.add(new Line(level, message, t));
} }
} }
}
/** /**
* Switch from deferred logging to immediate logging to the specified destination. * Switch from deferred logging to immediate logging to the specified destination.

@ -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 {
@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);
} }

Loading…
Cancel
Save