Skip to content

Commit cbde6ed

Browse files
committed
Merge pull request spring-projects#8474 from Nowheresly:spring-projectsgh-8396
* pr/8474: Polish contribution Add Tomcat Access Log's fileDateFormat property
2 parents b5fc68d + f8bf05b commit cbde6ed

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,6 +1006,7 @@ private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) {
10061006
this.accesslog.isRequestAttributesEnabled());
10071007
valve.setRotatable(this.accesslog.isRotate());
10081008
valve.setBuffered(this.accesslog.isBuffered());
1009+
valve.setFileDateFormat(this.accesslog.getFileDateFormat());
10091010
factory.addEngineValves(valve);
10101011
}
10111012

@@ -1060,6 +1061,11 @@ public static class Accesslog {
10601061
*/
10611062
private boolean renameOnRotate;
10621063

1064+
/**
1065+
* Date format to place in log file name.
1066+
*/
1067+
private String fileDateFormat = ".yyyy-MM-dd";
1068+
10631069
/**
10641070
* Set request attributes for IP address, Hostname, protocol and port used for
10651071
* the request.
@@ -1127,6 +1133,14 @@ public void setRenameOnRotate(boolean renameOnRotate) {
11271133
this.renameOnRotate = renameOnRotate;
11281134
}
11291135

1136+
public String getFileDateFormat() {
1137+
return this.fileDateFormat;
1138+
}
1139+
1140+
public void setFileDateFormat(String fileDateFormat) {
1141+
this.fileDateFormat = fileDateFormat;
1142+
}
1143+
11301144
public boolean isRequestAttributesEnabled() {
11311145
return this.requestAttributesEnabled;
11321146
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,29 @@ public void tomcatAccessLogCanBeEnabled() {
160160
.isInstanceOf(AccessLogValve.class);
161161
}
162162

163+
@Test
164+
public void tomcatAccessLogFileDateFormatByDefault() {
165+
TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory();
166+
Map<String, String> map = new HashMap<String, String>();
167+
map.put("server.tomcat.accesslog.enabled", "true");
168+
bindProperties(map);
169+
this.properties.customize(tomcatContainer);
170+
assertThat(((AccessLogValve) tomcatContainer.getEngineValves().iterator().next())
171+
.getFileDateFormat()).isEqualTo(".yyyy-MM-dd");
172+
}
173+
174+
@Test
175+
public void tomcatAccessLogFileDateFormatCanBeRedefined() {
176+
TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory();
177+
Map<String, String> map = new HashMap<String, String>();
178+
map.put("server.tomcat.accesslog.enabled", "true");
179+
map.put("server.tomcat.accesslog.file-date-format", "yyyy-MM-dd.HH");
180+
bindProperties(map);
181+
this.properties.customize(tomcatContainer);
182+
assertThat(((AccessLogValve) tomcatContainer.getEngineValves().iterator().next())
183+
.getFileDateFormat()).isEqualTo("yyyy-MM-dd.HH");
184+
}
185+
163186
@Test
164187
public void tomcatAccessLogIsBufferedByDefault() {
165188
TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory();

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ content into your application; rather pick only the properties that you need.
196196
server.tomcat.accesslog.buffered=true # Buffer output such that it is only flushed periodically.
197197
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be relative to the tomcat base dir or absolute.
198198
server.tomcat.accesslog.enabled=false # Enable access log.
199+
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in log file name.
199200
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
200201
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
201202
server.tomcat.accesslog.rename-on-rotate=false # Defer inclusion of the date stamp in the file name until rotate time.

0 commit comments

Comments
 (0)