Skip to content

Commit

Permalink
Make Tomcat Access Log's buffering configurable via the environment
Browse files Browse the repository at this point in the history
Closes gh-7456
  • Loading branch information
wilkinsona committed Dec 2, 2016
1 parent b546fd1 commit b60150b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
Expand Up @@ -1005,6 +1005,7 @@ private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) {
valve.setRequestAttributesEnabled(
this.accesslog.isRequestAttributesEnabled());
valve.setRotatable(this.accesslog.isRotate());
valve.setBuffered(this.accesslog.isBuffered());
factory.addEngineValves(valve);
}

Expand Down Expand Up @@ -1065,6 +1066,11 @@ public static class Accesslog {
*/
private boolean requestAttributesEnabled;

/**
* Buffer output such that it is only flushed periodically.
*/
private boolean buffered = true;

public boolean isEnabled() {
return this.enabled;
}
Expand Down Expand Up @@ -1129,6 +1135,14 @@ public void setRequestAttributesEnabled(boolean requestAttributesEnabled) {
this.requestAttributesEnabled = requestAttributesEnabled;
}

public boolean isBuffered() {
return this.buffered;
}

public void setBuffered(boolean buffered) {
this.buffered = buffered;
}

}

}
Expand Down
Expand Up @@ -31,6 +31,7 @@

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;
Expand Down Expand Up @@ -140,6 +141,48 @@ public void testServletPathAsPrefix() throws Exception {
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<String, String> map = new HashMap<String, String>();
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<String, String> map = new HashMap<String, String>();
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<String, String> map = new HashMap<String, String>();
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<String, String> map = new HashMap<String, String>();
Expand Down

0 comments on commit b60150b

Please sign in to comment.