Skip to content

Make Tomcat Access Log's fileDateFormat configurable via the environment #8474

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) {
this.accesslog.isRequestAttributesEnabled());
valve.setRotatable(this.accesslog.isRotate());
valve.setBuffered(this.accesslog.isBuffered());
valve.setFileDateFormat(this.accesslog.getFileDateFormat());
factory.addEngineValves(valve);
}

Expand Down Expand Up @@ -1071,6 +1072,11 @@ public static class Accesslog {
*/
private boolean buffered = true;

/**
* Customized date format in the access log file name.
*/
private String fileDateFormat = "yyyy-MM-dd";

public boolean isEnabled() {
return this.enabled;
}
Expand Down Expand Up @@ -1143,6 +1149,13 @@ public void setBuffered(boolean buffered) {
this.buffered = buffered;
}

public String getFileDateFormat() {
return fileDateFormat;
}

public void setFileDateFormat(String fileDateFormat) {
this.fileDateFormat = fileDateFormat;
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,40 @@ public void tomcatAccessLogCanBeEnabled() {
.isInstanceOf(AccessLogValve.class);
}

@Test
public void tomcatAccessLogFileDateFormatByDefault() {
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())
.getFileDateFormat()).isEqualTo("yyyy-MM-dd");
}

@Test
public void tomcatAccessLogFileDateFormatCanBeRedefined() {
TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory();
Map<String, String> map = new HashMap<String, String>();
map.put("server.tomcat.accesslog.enabled", "true");
map.put("server.tomcat.accesslog.file-date-format", "yyyy-MM-dd.HH");
bindProperties(map);
this.properties.customize(tomcatContainer);
assertThat(((AccessLogValve) tomcatContainer.getEngineValves().iterator().next())
.getFileDateFormat()).isEqualTo("yyyy-MM-dd.HH");
}

@Test(expected = IllegalArgumentException.class)
public void tomcatAccessLogFileDateFormatWrongFormat() {
TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory();
Map<String, String> map = new HashMap<String, String>();
map.put("server.tomcat.accesslog.enabled", "true");
map.put("server.tomcat.accesslog.file-date-format",
"this-is-obviously-a-wrong-format");
bindProperties(map);
this.properties.customize(tomcatContainer);
}

@Test
public void tomcatAccessLogIsBufferedByDefault() {
TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ content into your application; rather pick only the properties that you need.
server.tomcat.accesslog.buffered=true # Buffer output such that it is only flushed periodically.
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
server.tomcat.accesslog.enabled=false # Enable access log.
server.tomcat.accesslog.file-date-format=yyyy-MM-dd # Customized date format in the access log file name.
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.
Expand Down