From a6ef3741efefb1c7b19c453454cfd17a87de1a69 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 22 Aug 2016 10:12:26 +0200 Subject: [PATCH] Configure Undertow's access log prefix and suffix This commit adds two properties that can be used to customize the prefix and suffix of the Undertow's access log. Closes gh-6652 --- .../autoconfigure/web/ServerProperties.java | 32 +++++++++++++++++++ .../appendix-application-properties.adoc | 2 ++ ...dertowEmbeddedServletContainerFactory.java | 15 ++++++++- ...wEmbeddedServletContainerFactoryTests.java | 15 ++++++++- 4 files changed, 62 insertions(+), 2 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 c719cf0db2..13b17dee4f 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 @@ -1270,6 +1270,12 @@ public class ServerProperties if (this.accesslog.pattern != null) { factory.setAccessLogPattern(this.accesslog.pattern); } + if (this.accesslog.prefix != null) { + factory.setAccessLogPrefix(this.accesslog.prefix); + } + if (this.accesslog.suffix != null) { + factory.setAccessLogSuffix(this.accesslog.suffix); + } if (this.accesslog.enabled != null) { factory.setAccessLogEnabled(this.accesslog.enabled); } @@ -1340,6 +1346,16 @@ public class ServerProperties */ private String pattern = "common"; + /** + * Log file name prefix. + */ + protected String prefix = "access_log."; + + /** + * Log file name suffix. + */ + private String suffix = "log"; + /** * Undertow access log directory. */ @@ -1361,6 +1377,22 @@ public class ServerProperties this.pattern = pattern; } + public String getPrefix() { + return this.prefix; + } + + public void setPrefix(String prefix) { + this.prefix = prefix; + } + + public String getSuffix() { + return this.suffix; + } + + public void setSuffix(String suffix) { + this.suffix = suffix; + } + public File getDir() { return this.dir; } 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 2646d3baff..7f48424834 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -219,6 +219,8 @@ content into your application; rather pick only the properties that you need. server.undertow.accesslog.dir= # Undertow access log directory. server.undertow.accesslog.enabled=false # Enable access log. server.undertow.accesslog.pattern=common # Format pattern for access logs. + server.undertow.accesslog.prefix=access_log. # Log file name prefix. + server.undertow.accesslog.suffix=log # Log file name suffix. server.undertow.buffer-size= # Size of each buffer in bytes. server.undertow.buffers-per-region= # Number of buffer per region. server.undertow.direct-buffers= # Allocate buffers outside the Java heap. diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java index f28725d1fc..39af58e6c6 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java @@ -122,6 +122,10 @@ public class UndertowEmbeddedServletContainerFactory private String accessLogPattern; + private String accessLogPrefix; + + private String accessLogSuffix; + private boolean accessLogEnabled = false; private boolean useForwardHeaders; @@ -408,8 +412,9 @@ public class UndertowEmbeddedServletContainerFactory private AccessLogHandler createAccessLogHandler(HttpHandler handler) { try { createAccessLogDirectoryIfNecessary(); + String prefix = (this.accessLogPrefix != null ? this.accessLogPrefix : "access_log."); AccessLogReceiver accessLogReceiver = new DefaultAccessLogReceiver( - createWorker(), this.accessLogDirectory, "access_log."); + createWorker(), this.accessLogDirectory, prefix, this.accessLogSuffix); String formatString = (this.accessLogPattern != null) ? this.accessLogPattern : "common"; return new AccessLogHandler(handler, accessLogReceiver, formatString, @@ -561,6 +566,14 @@ public class UndertowEmbeddedServletContainerFactory this.accessLogPattern = accessLogPattern; } + public void setAccessLogPrefix(String accessLogPrefix) { + this.accessLogPrefix = accessLogPrefix; + } + + public void setAccessLogSuffix(String accessLogSuffix) { + this.accessLogSuffix = accessLogSuffix; + } + public void setAccessLogEnabled(boolean accessLogEnabled) { this.accessLogEnabled = accessLogEnabled; } diff --git a/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java b/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java index 5c1be361aa..2fff72cea4 100644 --- a/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactoryTests.java @@ -178,8 +178,21 @@ public class UndertowEmbeddedServletContainerFactoryTests @Test public void accessLogCanBeEnabled() throws IOException, URISyntaxException, InterruptedException { + testAccessLog(null, null, "access_log.log"); + } + + @Test + public void accessLogCanBeCustomized() + throws IOException, URISyntaxException, InterruptedException { + testAccessLog("my_access.", "logz", "my_access.logz"); + } + + private void testAccessLog(String prefix, String suffix, String expectedFile) + throws IOException, URISyntaxException, InterruptedException { UndertowEmbeddedServletContainerFactory factory = getFactory(); factory.setAccessLogEnabled(true); + factory.setAccessLogPrefix(prefix); + factory.setAccessLogSuffix(suffix); File accessLogDirectory = this.temporaryFolder.getRoot(); factory.setAccessLogDirectory(accessLogDirectory); assertThat(accessLogDirectory.listFiles()).isEmpty(); @@ -187,7 +200,7 @@ public class UndertowEmbeddedServletContainerFactoryTests new ServletRegistrationBean(new ExampleServlet(), "/hello")); this.container.start(); assertThat(getResponse(getLocalUrl("/hello"))).isEqualTo("Hello World"); - File accessLog = new File(accessLogDirectory, "access_log.log"); + File accessLog = new File(accessLogDirectory, expectedFile); awaitFile(accessLog); assertThat(accessLogDirectory.listFiles()).contains(accessLog); }