From b60150b05ea19f2db52773fa809e9900b28a2df0 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 2 Dec 2016 13:58:26 +0000 Subject: [PATCH] Make Tomcat Access Log's buffering configurable via the environment Closes gh-7456 --- .../autoconfigure/web/ServerProperties.java | 14 ++++++ .../web/ServerPropertiesTests.java | 43 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 20eea271ccb..7e98fa3b218 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -1005,6 +1005,7 @@ public class ServerProperties valve.setRequestAttributesEnabled( this.accesslog.isRequestAttributesEnabled()); valve.setRotatable(this.accesslog.isRotate()); + valve.setBuffered(this.accesslog.isBuffered()); factory.addEngineValves(valve); } @@ -1065,6 +1066,11 @@ public class ServerProperties */ private boolean requestAttributesEnabled; + /** + * Buffer output such that it is only flushed periodically. + */ + private boolean buffered = true; + public boolean isEnabled() { return this.enabled; } @@ -1129,6 +1135,14 @@ public class ServerProperties this.requestAttributesEnabled = requestAttributesEnabled; } + public boolean isBuffered() { + return this.buffered; + } + + public void setBuffered(boolean buffered) { + this.buffered = buffered; + } + } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index deb95eb1f98..b8e5fa9b1d4 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -31,6 +31,7 @@ import javax.servlet.SessionTrackingMode; import org.apache.catalina.Context; import org.apache.catalina.Valve; +import org.apache.catalina.valves.AccessLogValve; import org.apache.catalina.valves.RemoteIpValve; import org.apache.coyote.AbstractProtocol; import org.junit.Before; @@ -140,6 +141,48 @@ public class ServerPropertiesTests { assertThat(this.properties.getServletPrefix()).isEqualTo("/foo"); } + @Test + public void tomcatAccessLogIsDisabledByDefault() { + TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory(); + this.properties.customize(tomcatContainer); + assertThat(tomcatContainer.getEngineValves()).isEmpty(); + } + + @Test + public void tomcatAccessLogCanBeEnabled() { + TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory(); + Map map = new HashMap(); + map.put("server.tomcat.accesslog.enabled", "true"); + bindProperties(map); + this.properties.customize(tomcatContainer); + assertThat(tomcatContainer.getEngineValves()).hasSize(1); + assertThat(tomcatContainer.getEngineValves()).first() + .isInstanceOf(AccessLogValve.class); + } + + @Test + public void tomcatAccessLogIsBufferedByDefault() { + TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory(); + Map map = new HashMap(); + map.put("server.tomcat.accesslog.enabled", "true"); + bindProperties(map); + this.properties.customize(tomcatContainer); + assertThat(((AccessLogValve) tomcatContainer.getEngineValves().iterator().next()) + .isBuffered()).isTrue(); + } + + @Test + public void tomcatAccessLogBufferingCanBeDisabled() { + TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory(); + Map map = new HashMap(); + map.put("server.tomcat.accesslog.enabled", "true"); + map.put("server.tomcat.accesslog.buffered", "false"); + bindProperties(map); + this.properties.customize(tomcatContainer); + assertThat(((AccessLogValve) tomcatContainer.getEngineValves().iterator().next()) + .isBuffered()).isFalse(); + } + @Test public void testTomcatBinding() throws Exception { Map map = new HashMap();