Skip to content

Commit 596f0c2

Browse files
nosansnicoll
authored andcommitted
Expose Tomcat AccessLog Max days property
See gh-15954
1 parent be40d00 commit 596f0c2

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,19 @@ public static class Accesslog {
609609
*/
610610
private boolean buffered = true;
611611

612+
/**
613+
* The number of days to retain the access log files before they are removed.
614+
*/
615+
private int maxDays = -1;
616+
617+
public int getMaxDays() {
618+
return this.maxDays;
619+
}
620+
621+
public void setMaxDays(int maxDays) {
622+
this.maxDays = maxDays;
623+
}
624+
612625
public boolean isEnabled() {
613626
return this.enabled;
614627
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ private void customizeAccessLog(ConfigurableTomcatWebServerFactory factory) {
263263
tomcatProperties.getAccesslog().isRequestAttributesEnabled());
264264
valve.setRotatable(tomcatProperties.getAccesslog().isRotate());
265265
valve.setBuffered(tomcatProperties.getAccesslog().isBuffered());
266+
valve.setMaxDays(tomcatProperties.getAccesslog().getMaxDays());
266267
factory.addEngineValves(valve);
267268
}
268269

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,23 @@ public void accessLogIsDisabledByDefault() {
325325
assertThat(factory.getEngineValves()).isEmpty();
326326
}
327327

328+
@Test
329+
public void accessLogSetMaxDays() {
330+
bind("server.tomcat.accesslog.enabled=true",
331+
"server.tomcat.accesslog.max-days=20");
332+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
333+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
334+
.getMaxDays()).isEqualTo(20);
335+
}
336+
337+
@Test
338+
public void accessLogDefaultMaxDays() {
339+
bind("server.tomcat.accesslog.enabled=true");
340+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
341+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
342+
.getMaxDays()).isEqualTo(-1);
343+
}
344+
328345
private void bind(String... inlinedProperties) {
329346
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
330347
inlinedProperties);

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ content into your application. Rather, pick only the properties that you need.
255255
server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir.
256256
server.tomcat.accesslog.enabled=false # Enable access log.
257257
server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name.
258+
server.tomcat.accesslog.max-days=-1#The number of days to retain the access log files before they are removed.
258259
server.tomcat.accesslog.pattern=common # Format pattern for access logs.
259260
server.tomcat.accesslog.prefix=access_log # Log file name prefix.
260261
server.tomcat.accesslog.rename-on-rotate=false # Whether to defer inclusion of the date stamp in the file name until rotate time.

0 commit comments

Comments
 (0)