From 596f0c28c0edff1f365f465461de216d63e009f7 Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Thu, 14 Feb 2019 13:35:25 +0200 Subject: [PATCH 1/2] Expose Tomcat AccessLog Max days property See gh-15954 --- .../autoconfigure/web/ServerProperties.java | 13 +++++++++++++ .../TomcatWebServerFactoryCustomizer.java | 1 + .../TomcatWebServerFactoryCustomizerTests.java | 17 +++++++++++++++++ .../appendix/application-properties.adoc | 1 + 4 files changed, 32 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 682dda50c0..65f0d51f8a 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -609,6 +609,19 @@ public class ServerProperties { */ private boolean buffered = true; + /** + * The number of days to retain the access log files before they are removed. + */ + private int maxDays = -1; + + public int getMaxDays() { + return this.maxDays; + } + + public void setMaxDays(int maxDays) { + this.maxDays = maxDays; + } + public boolean isEnabled() { return this.enabled; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java index 04b965d1c9..a20cdaa91e 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java @@ -263,6 +263,7 @@ public class TomcatWebServerFactoryCustomizer implements tomcatProperties.getAccesslog().isRequestAttributesEnabled()); valve.setRotatable(tomcatProperties.getAccesslog().isRotate()); valve.setBuffered(tomcatProperties.getAccesslog().isBuffered()); + valve.setMaxDays(tomcatProperties.getAccesslog().getMaxDays()); factory.addEngineValves(valve); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java index c68c1e0c7f..202a0828a6 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java @@ -325,6 +325,23 @@ public class TomcatWebServerFactoryCustomizerTests { assertThat(factory.getEngineValves()).isEmpty(); } + @Test + public void accessLogSetMaxDays() { + bind("server.tomcat.accesslog.enabled=true", + "server.tomcat.accesslog.max-days=20"); + TomcatServletWebServerFactory factory = customizeAndGetFactory(); + assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()) + .getMaxDays()).isEqualTo(20); + } + + @Test + public void accessLogDefaultMaxDays() { + bind("server.tomcat.accesslog.enabled=true"); + TomcatServletWebServerFactory factory = customizeAndGetFactory(); + assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()) + .getMaxDays()).isEqualTo(-1); + } + private void bind(String... inlinedProperties) { TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment, inlinedProperties); diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc index 2cf97e29e8..1b027edafb 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc @@ -255,6 +255,7 @@ content into your application. Rather, pick only the properties that you need. server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir. server.tomcat.accesslog.enabled=false # Enable access log. server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name. + server.tomcat.accesslog.max-days=-1#The number of days to retain the access log files before they are removed. 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 # Whether to defer inclusion of the date stamp in the file name until rotate time. From 71df2f311b04462784a76ed0b9288f2d659a68eb Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Thu, 14 Feb 2019 15:54:51 +0100 Subject: [PATCH 2/2] Polish "Expose Tomcat AccessLog Max days property" Closes gh-15954 --- .../autoconfigure/web/ServerProperties.java | 28 +++++++++---------- .../TomcatWebServerFactoryCustomizer.java | 4 +-- ...TomcatWebServerFactoryCustomizerTests.java | 17 +++++------ .../appendix/application-properties.adoc | 2 +- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 65f0d51f8a..b667911e2c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -593,6 +593,11 @@ public class ServerProperties { */ private boolean renameOnRotate = false; + /** + * Number of days to retain the access log files before they are removed. + */ + private int maxDays = -1; + /** * Date format to place in the log file name. */ @@ -609,19 +614,6 @@ public class ServerProperties { */ private boolean buffered = true; - /** - * The number of days to retain the access log files before they are removed. - */ - private int maxDays = -1; - - public int getMaxDays() { - return this.maxDays; - } - - public void setMaxDays(int maxDays) { - this.maxDays = maxDays; - } - public boolean isEnabled() { return this.enabled; } @@ -678,6 +670,14 @@ public class ServerProperties { this.renameOnRotate = renameOnRotate; } + public int getMaxDays() { + return this.maxDays; + } + + public void setMaxDays(int maxDays) { + this.maxDays = maxDays; + } + public String getFileDateFormat() { return this.fileDateFormat; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java index a20cdaa91e..3bfc2590e7 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -258,12 +258,12 @@ public class TomcatWebServerFactoryCustomizer implements valve.setPrefix(tomcatProperties.getAccesslog().getPrefix()); valve.setSuffix(tomcatProperties.getAccesslog().getSuffix()); valve.setRenameOnRotate(tomcatProperties.getAccesslog().isRenameOnRotate()); + valve.setMaxDays(tomcatProperties.getAccesslog().getMaxDays()); valve.setFileDateFormat(tomcatProperties.getAccesslog().getFileDateFormat()); valve.setRequestAttributesEnabled( tomcatProperties.getAccesslog().isRequestAttributesEnabled()); valve.setRotatable(tomcatProperties.getAccesslog().isRotate()); valve.setBuffered(tomcatProperties.getAccesslog().isBuffered()); - valve.setMaxDays(tomcatProperties.getAccesslog().getMaxDays()); factory.addEngineValves(valve); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java index 202a0828a6..f669434abf 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2018 the original author or authors. + * Copyright 2012-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -326,20 +326,21 @@ public class TomcatWebServerFactoryCustomizerTests { } @Test - public void accessLogSetMaxDays() { - bind("server.tomcat.accesslog.enabled=true", - "server.tomcat.accesslog.max-days=20"); + public void accessLogMaxDaysDefault() { + bind("server.tomcat.accesslog.enabled=true"); TomcatServletWebServerFactory factory = customizeAndGetFactory(); assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()) - .getMaxDays()).isEqualTo(20); + .getMaxDays()).isEqualTo( + this.serverProperties.getTomcat().getAccesslog().getMaxDays()); } @Test - public void accessLogDefaultMaxDays() { - bind("server.tomcat.accesslog.enabled=true"); + public void accessLoMaxDaysCanBeRedefined() { + bind("server.tomcat.accesslog.enabled=true", + "server.tomcat.accesslog.max-days=20"); TomcatServletWebServerFactory factory = customizeAndGetFactory(); assertThat(((AccessLogValve) factory.getEngineValves().iterator().next()) - .getMaxDays()).isEqualTo(-1); + .getMaxDays()).isEqualTo(20); } private void bind(String... inlinedProperties) { diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc index 1b027edafb..a87cb83cc0 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/appendix/application-properties.adoc @@ -255,7 +255,7 @@ content into your application. Rather, pick only the properties that you need. server.tomcat.accesslog.directory=logs # Directory in which log files are created. Can be absolute or relative to the Tomcat base dir. server.tomcat.accesslog.enabled=false # Enable access log. server.tomcat.accesslog.file-date-format=.yyyy-MM-dd # Date format to place in the log file name. - server.tomcat.accesslog.max-days=-1#The number of days to retain the access log files before they are removed. + server.tomcat.accesslog.max-days=-1# Number of days to retain the access log files before they are removed. 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 # Whether to defer inclusion of the date stamp in the file name until rotate time.