From 0ddaca57fb4d9810153a529c4153d45c24f02630 Mon Sep 17 00:00:00 2001 From: srichard Date: Thu, 2 Mar 2017 20:56:57 +0100 Subject: [PATCH 1/2] Add Tomcat Access Log's fileDateFormat property Closes gh-8396 --- .../autoconfigure/web/ServerProperties.java | 13 +++++++ .../web/ServerPropertiesTests.java | 34 +++++++++++++++++++ .../appendix-application-properties.adoc | 1 + 3 files changed, 48 insertions(+) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 9830d0e87db..5c0e9868ab1 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -1006,6 +1006,7 @@ public class ServerProperties this.accesslog.isRequestAttributesEnabled()); valve.setRotatable(this.accesslog.isRotate()); valve.setBuffered(this.accesslog.isBuffered()); + valve.setFileDateFormat(this.accesslog.getFileDateFormat()); factory.addEngineValves(valve); } @@ -1071,6 +1072,11 @@ public class ServerProperties */ 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; } @@ -1143,6 +1149,13 @@ public class ServerProperties this.buffered = buffered; } + public String getFileDateFormat() { + return fileDateFormat; + } + + public void setFileDateFormat(String fileDateFormat) { + this.fileDateFormat = fileDateFormat; + } } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index f9df7a8314c..dc3c67d66d7 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -160,6 +160,40 @@ public class ServerPropertiesTests { .isInstanceOf(AccessLogValve.class); } + @Test + public void tomcatAccessLogFileDateFormatByDefault() { + TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory(); + Map map = new HashMap(); + 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 map = new HashMap(); + 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 map = new HashMap(); + 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(); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 829aa371fc4..d27e0e6cd1f 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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. From f8bf05b91fcb9ea2a8501fcb9436214b871b4e8d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Fri, 10 Mar 2017 10:20:48 +0100 Subject: [PATCH 2/2] Polish contribution Closes gh-8474 --- .../autoconfigure/web/ServerProperties.java | 25 ++++++++++--------- .../web/ServerPropertiesTests.java | 13 +--------- .../appendix-application-properties.adoc | 2 +- 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 5c0e9868ab1..d4e2d4ba876 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -1061,6 +1061,11 @@ public class ServerProperties */ private boolean renameOnRotate; + /** + * Date format to place in log file name. + */ + private String fileDateFormat = ".yyyy-MM-dd"; + /** * Set request attributes for IP address, Hostname, protocol and port used for * the request. @@ -1072,11 +1077,6 @@ public class ServerProperties */ 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; } @@ -1133,6 +1133,14 @@ public class ServerProperties this.renameOnRotate = renameOnRotate; } + public String getFileDateFormat() { + return this.fileDateFormat; + } + + public void setFileDateFormat(String fileDateFormat) { + this.fileDateFormat = fileDateFormat; + } + public boolean isRequestAttributesEnabled() { return this.requestAttributesEnabled; } @@ -1149,13 +1157,6 @@ public class ServerProperties this.buffered = buffered; } - public String getFileDateFormat() { - return fileDateFormat; - } - - public void setFileDateFormat(String fileDateFormat) { - this.fileDateFormat = fileDateFormat; - } } } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java index dc3c67d66d7..e6819d95c1e 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java @@ -168,7 +168,7 @@ public class ServerPropertiesTests { bindProperties(map); this.properties.customize(tomcatContainer); assertThat(((AccessLogValve) tomcatContainer.getEngineValves().iterator().next()) - .getFileDateFormat()).isEqualTo("yyyy-MM-dd"); + .getFileDateFormat()).isEqualTo(".yyyy-MM-dd"); } @Test @@ -183,17 +183,6 @@ public class ServerPropertiesTests { .getFileDateFormat()).isEqualTo("yyyy-MM-dd.HH"); } - @Test(expected = IllegalArgumentException.class) - public void tomcatAccessLogFileDateFormatWrongFormat() { - TomcatEmbeddedServletContainerFactory tomcatContainer = new TomcatEmbeddedServletContainerFactory(); - Map map = new HashMap(); - 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(); diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index d27e0e6cd1f..948e750ad02 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -196,7 +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.file-date-format=.yyyy-MM-dd # Date format to place in 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.