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
pull/6721/head
Stephane Nicoll 8 years ago
parent 9420daf001
commit a6ef3741ef

@ -1270,6 +1270,12 @@ public class ServerProperties
if (this.accesslog.pattern != null) { if (this.accesslog.pattern != null) {
factory.setAccessLogPattern(this.accesslog.pattern); 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) { if (this.accesslog.enabled != null) {
factory.setAccessLogEnabled(this.accesslog.enabled); factory.setAccessLogEnabled(this.accesslog.enabled);
} }
@ -1340,6 +1346,16 @@ public class ServerProperties
*/ */
private String pattern = "common"; 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. * Undertow access log directory.
*/ */
@ -1361,6 +1377,22 @@ public class ServerProperties
this.pattern = pattern; 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() { public File getDir() {
return this.dir; return this.dir;
} }

@ -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.dir= # Undertow access log directory.
server.undertow.accesslog.enabled=false # Enable access log. server.undertow.accesslog.enabled=false # Enable access log.
server.undertow.accesslog.pattern=common # Format pattern for access logs. 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.buffer-size= # Size of each buffer in bytes.
server.undertow.buffers-per-region= # Number of buffer per region. server.undertow.buffers-per-region= # Number of buffer per region.
server.undertow.direct-buffers= # Allocate buffers outside the Java heap. server.undertow.direct-buffers= # Allocate buffers outside the Java heap.

@ -122,6 +122,10 @@ public class UndertowEmbeddedServletContainerFactory
private String accessLogPattern; private String accessLogPattern;
private String accessLogPrefix;
private String accessLogSuffix;
private boolean accessLogEnabled = false; private boolean accessLogEnabled = false;
private boolean useForwardHeaders; private boolean useForwardHeaders;
@ -408,8 +412,9 @@ public class UndertowEmbeddedServletContainerFactory
private AccessLogHandler createAccessLogHandler(HttpHandler handler) { private AccessLogHandler createAccessLogHandler(HttpHandler handler) {
try { try {
createAccessLogDirectoryIfNecessary(); createAccessLogDirectoryIfNecessary();
String prefix = (this.accessLogPrefix != null ? this.accessLogPrefix : "access_log.");
AccessLogReceiver accessLogReceiver = new DefaultAccessLogReceiver( AccessLogReceiver accessLogReceiver = new DefaultAccessLogReceiver(
createWorker(), this.accessLogDirectory, "access_log."); createWorker(), this.accessLogDirectory, prefix, this.accessLogSuffix);
String formatString = (this.accessLogPattern != null) ? this.accessLogPattern String formatString = (this.accessLogPattern != null) ? this.accessLogPattern
: "common"; : "common";
return new AccessLogHandler(handler, accessLogReceiver, formatString, return new AccessLogHandler(handler, accessLogReceiver, formatString,
@ -561,6 +566,14 @@ public class UndertowEmbeddedServletContainerFactory
this.accessLogPattern = accessLogPattern; this.accessLogPattern = accessLogPattern;
} }
public void setAccessLogPrefix(String accessLogPrefix) {
this.accessLogPrefix = accessLogPrefix;
}
public void setAccessLogSuffix(String accessLogSuffix) {
this.accessLogSuffix = accessLogSuffix;
}
public void setAccessLogEnabled(boolean accessLogEnabled) { public void setAccessLogEnabled(boolean accessLogEnabled) {
this.accessLogEnabled = accessLogEnabled; this.accessLogEnabled = accessLogEnabled;
} }

@ -178,8 +178,21 @@ public class UndertowEmbeddedServletContainerFactoryTests
@Test @Test
public void accessLogCanBeEnabled() public void accessLogCanBeEnabled()
throws IOException, URISyntaxException, InterruptedException { 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(); UndertowEmbeddedServletContainerFactory factory = getFactory();
factory.setAccessLogEnabled(true); factory.setAccessLogEnabled(true);
factory.setAccessLogPrefix(prefix);
factory.setAccessLogSuffix(suffix);
File accessLogDirectory = this.temporaryFolder.getRoot(); File accessLogDirectory = this.temporaryFolder.getRoot();
factory.setAccessLogDirectory(accessLogDirectory); factory.setAccessLogDirectory(accessLogDirectory);
assertThat(accessLogDirectory.listFiles()).isEmpty(); assertThat(accessLogDirectory.listFiles()).isEmpty();
@ -187,7 +200,7 @@ public class UndertowEmbeddedServletContainerFactoryTests
new ServletRegistrationBean(new ExampleServlet(), "/hello")); new ServletRegistrationBean(new ExampleServlet(), "/hello"));
this.container.start(); this.container.start();
assertThat(getResponse(getLocalUrl("/hello"))).isEqualTo("Hello World"); assertThat(getResponse(getLocalUrl("/hello"))).isEqualTo("Hello World");
File accessLog = new File(accessLogDirectory, "access_log.log"); File accessLog = new File(accessLogDirectory, expectedFile);
awaitFile(accessLog); awaitFile(accessLog);
assertThat(accessLogDirectory.listFiles()).contains(accessLog); assertThat(accessLogDirectory.listFiles()).contains(accessLog);
} }

Loading…
Cancel
Save