Improve customization of Tomcat's access log
Add `directory`, `prefix` and `suffix` properties to further customize how access logs are configured on Tomcat. Relocate all properties to the `server.tomcat.accesslog` namespace. `server.tomcat.accessLogPattern` and `server.tomcat.accessLogEnabled` are deprecated and replaced by `server.tomcat.accesslog.pattern` and `server.tomcat.accesslog.enabled` respectively. Closes gh-2491
This commit is contained in:
parent
4a25bae143
commit
62406546e2
|
|
@ -467,14 +467,9 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||||
public static class Tomcat {
|
public static class Tomcat {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format pattern for access logs.
|
* Access log configuration
|
||||||
*/
|
*/
|
||||||
private String accessLogPattern;
|
private final Accesslog accesslog = new Accesslog();
|
||||||
|
|
||||||
/**
|
|
||||||
* Enable access log.
|
|
||||||
*/
|
|
||||||
private boolean accessLogEnabled = false;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Regular expression that matches proxies that are to be trusted.
|
* Regular expression that matches proxies that are to be trusted.
|
||||||
|
|
@ -550,12 +545,50 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||||
this.maxHttpHeaderSize = maxHttpHeaderSize;
|
this.maxHttpHeaderSize = maxHttpHeaderSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getAccessLogEnabled() {
|
public Accesslog getAccesslog() {
|
||||||
return this.accessLogEnabled;
|
return this.accesslog;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Specify if access log is enabled.
|
||||||
|
* @return {@code true} if access log is enabled
|
||||||
|
* @deprecated since 1.3.0 in favor of {@code server.tomcat.accesslog.enabled}
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@DeprecatedConfigurationProperty(replacement = "server.tomcat.accesslog.enabled")
|
||||||
|
public boolean getAccessLogEnabled() {
|
||||||
|
return this.accesslog.isEnabled();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set if access log is enabled.
|
||||||
|
* @param accessLogEnabled the access log enable flag
|
||||||
|
* @deprecated since 1.3.0 in favor of {@code server.tomcat.accesslog.enabled}
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
public void setAccessLogEnabled(boolean accessLogEnabled) {
|
public void setAccessLogEnabled(boolean accessLogEnabled) {
|
||||||
this.accessLogEnabled = accessLogEnabled;
|
getAccesslog().setEnabled(accessLogEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the format pattern for access logs.
|
||||||
|
* @return the format pattern for access logs
|
||||||
|
* @deprecated since 1.3.0 in favor of {@code server.tomcat.accesslog.pattern}
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
@DeprecatedConfigurationProperty(replacement = "server.tomcat.accesslog.pattern")
|
||||||
|
public String getAccessLogPattern() {
|
||||||
|
return this.accesslog.getPattern();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the format pattern for access logs
|
||||||
|
* @param accessLogPattern the pattern for access logs
|
||||||
|
* @deprecated since 1.3.0 in favor of {@code server.tomcat.accesslog.pattern}
|
||||||
|
*/
|
||||||
|
@Deprecated
|
||||||
|
public void setAccessLogPattern(String accessLogPattern) {
|
||||||
|
this.accesslog.setPattern(accessLogPattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getBackgroundProcessorDelay() {
|
public int getBackgroundProcessorDelay() {
|
||||||
|
|
@ -574,14 +607,6 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||||
this.basedir = basedir;
|
this.basedir = basedir;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAccessLogPattern() {
|
|
||||||
return this.accessLogPattern;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAccessLogPattern(String accessLogPattern) {
|
|
||||||
this.accessLogPattern = accessLogPattern;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getInternalProxies() {
|
public String getInternalProxies() {
|
||||||
return this.internalProxies;
|
return this.internalProxies;
|
||||||
}
|
}
|
||||||
|
|
@ -642,7 +667,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||||
if (this.maxHttpHeaderSize > 0) {
|
if (this.maxHttpHeaderSize > 0) {
|
||||||
customizeMaxHttpHeaderSize(factory);
|
customizeMaxHttpHeaderSize(factory);
|
||||||
}
|
}
|
||||||
if (this.accessLogEnabled) {
|
if (this.accesslog.enabled) {
|
||||||
customizeAccessLog(factory);
|
customizeAccessLog(factory);
|
||||||
}
|
}
|
||||||
if (getUriEncoding() != null) {
|
if (getUriEncoding() != null) {
|
||||||
|
|
@ -711,12 +736,87 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
||||||
}
|
}
|
||||||
|
|
||||||
private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) {
|
private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) {
|
||||||
AccessLogValve valve = new AccessLogValve();
|
factory.addContextValves(this.accesslog.createAccessLogValve());
|
||||||
String accessLogPattern = getAccessLogPattern();
|
|
||||||
valve.setPattern(accessLogPattern == null ? "common" : accessLogPattern);
|
|
||||||
valve.setSuffix(".log");
|
|
||||||
factory.addContextValves(valve);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class Accesslog {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable access log.
|
||||||
|
*/
|
||||||
|
private boolean enabled = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Format pattern for access logs.
|
||||||
|
*/
|
||||||
|
private String pattern = "common";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Directory in which log files are created. Can be relative to the tomcat
|
||||||
|
* base dir or absolute.
|
||||||
|
*/
|
||||||
|
private String directory = "logs";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log file name prefix.
|
||||||
|
*/
|
||||||
|
protected String prefix = "access_log";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Log file name suffix.
|
||||||
|
*/
|
||||||
|
private String suffix = ".log";
|
||||||
|
|
||||||
|
AccessLogValve createAccessLogValve() {
|
||||||
|
AccessLogValve valve = new AccessLogValve();
|
||||||
|
valve.setPattern(this.pattern);
|
||||||
|
valve.setDirectory(this.directory);
|
||||||
|
valve.setPrefix(this.prefix);
|
||||||
|
valve.setSuffix(this.suffix);
|
||||||
|
return valve;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPattern() {
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPattern(String pattern) {
|
||||||
|
this.pattern = pattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDirectory() {
|
||||||
|
return directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDirectory(String directory) {
|
||||||
|
this.directory = directory;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPrefix() {
|
||||||
|
return prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPrefix(String prefix) {
|
||||||
|
this.prefix = prefix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSuffix() {
|
||||||
|
return suffix;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSuffix(String suffix) {
|
||||||
|
this.suffix = suffix;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class Undertow {
|
public static class Undertow {
|
||||||
|
|
|
||||||
|
|
@ -110,18 +110,20 @@ public class ServerPropertiesTests {
|
||||||
@Test
|
@Test
|
||||||
public void testTomcatBinding() throws Exception {
|
public void testTomcatBinding() throws Exception {
|
||||||
Map<String, String> map = new HashMap<String, String>();
|
Map<String, String> map = new HashMap<String, String>();
|
||||||
map.put("server.tomcat.access_log_pattern", "%h %t '%r' %s %b");
|
map.put("server.tomcat.accesslog.pattern", "%h %t '%r' %s %b");
|
||||||
|
map.put("server.tomcat.accesslog.prefix", "foo");
|
||||||
|
map.put("server.tomcat.accesslog.suffix", "-bar.log");
|
||||||
map.put("server.tomcat.protocol_header", "X-Forwarded-Protocol");
|
map.put("server.tomcat.protocol_header", "X-Forwarded-Protocol");
|
||||||
map.put("server.tomcat.remote_ip_header", "Remote-Ip");
|
map.put("server.tomcat.remote_ip_header", "Remote-Ip");
|
||||||
map.put("server.tomcat.internal_proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
|
map.put("server.tomcat.internal_proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
|
||||||
bindProperties(map);
|
bindProperties(map);
|
||||||
assertEquals("%h %t '%r' %s %b", this.properties.getTomcat()
|
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
|
||||||
.getAccessLogPattern());
|
assertEquals("%h %t '%r' %s %b", tomcat.getAccesslog().getPattern());
|
||||||
assertEquals("Remote-Ip", this.properties.getTomcat().getRemoteIpHeader());
|
assertEquals("foo", tomcat.getAccesslog().getPrefix());
|
||||||
assertEquals("X-Forwarded-Protocol", this.properties.getTomcat()
|
assertEquals("-bar.log", tomcat.getAccesslog().getSuffix());
|
||||||
.getProtocolHeader());
|
assertEquals("Remote-Ip", tomcat.getRemoteIpHeader());
|
||||||
assertEquals("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", this.properties.getTomcat()
|
assertEquals("X-Forwarded-Protocol", tomcat.getProtocolHeader());
|
||||||
.getInternalProxies());
|
assertEquals("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", tomcat.getInternalProxies());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
|
|
@ -105,8 +105,11 @@ content into your application; rather pick only the properties that you need.
|
||||||
server.ssl.trust-store-password=
|
server.ssl.trust-store-password=
|
||||||
server.ssl.trust-store-provider=
|
server.ssl.trust-store-provider=
|
||||||
server.ssl.trust-store-type=
|
server.ssl.trust-store-type=
|
||||||
server.tomcat.access-log-pattern= # log pattern of the access log
|
server.tomcat.accesslog.directory=logs # directory in which log files are created
|
||||||
server.tomcat.access-log-enabled=false # is access logging enabled
|
server.tomcat.accesslog.enabled=false # is access logging enabled
|
||||||
|
server.tomcat.accesslog.pattern= # log pattern of the access log
|
||||||
|
server.tomcat.accesslog.prefix=access_log # log file name prefix
|
||||||
|
server.tomcat.accesslog.suffix=.log # log file name suffix
|
||||||
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
|
server.tomcat.internal-proxies=10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}|\\
|
||||||
192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
|
192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
|
||||||
169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
|
169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue