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 {
|
||||
|
||||
/**
|
||||
* Format pattern for access logs.
|
||||
* Access log configuration
|
||||
*/
|
||||
private String accessLogPattern;
|
||||
|
||||
/**
|
||||
* Enable access log.
|
||||
*/
|
||||
private boolean accessLogEnabled = false;
|
||||
private final Accesslog accesslog = new Accesslog();
|
||||
|
||||
/**
|
||||
* Regular expression that matches proxies that are to be trusted.
|
||||
|
|
@ -550,12 +545,50 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
|||
this.maxHttpHeaderSize = maxHttpHeaderSize;
|
||||
}
|
||||
|
||||
public boolean getAccessLogEnabled() {
|
||||
return this.accessLogEnabled;
|
||||
public Accesslog getAccesslog() {
|
||||
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) {
|
||||
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() {
|
||||
|
|
@ -574,14 +607,6 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
|||
this.basedir = basedir;
|
||||
}
|
||||
|
||||
public String getAccessLogPattern() {
|
||||
return this.accessLogPattern;
|
||||
}
|
||||
|
||||
public void setAccessLogPattern(String accessLogPattern) {
|
||||
this.accessLogPattern = accessLogPattern;
|
||||
}
|
||||
|
||||
public String getInternalProxies() {
|
||||
return this.internalProxies;
|
||||
}
|
||||
|
|
@ -642,7 +667,7 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
|||
if (this.maxHttpHeaderSize > 0) {
|
||||
customizeMaxHttpHeaderSize(factory);
|
||||
}
|
||||
if (this.accessLogEnabled) {
|
||||
if (this.accesslog.enabled) {
|
||||
customizeAccessLog(factory);
|
||||
}
|
||||
if (getUriEncoding() != null) {
|
||||
|
|
@ -711,12 +736,87 @@ public class ServerProperties implements EmbeddedServletContainerCustomizer, Ord
|
|||
}
|
||||
|
||||
private void customizeAccessLog(TomcatEmbeddedServletContainerFactory factory) {
|
||||
AccessLogValve valve = new AccessLogValve();
|
||||
String accessLogPattern = getAccessLogPattern();
|
||||
valve.setPattern(accessLogPattern == null ? "common" : accessLogPattern);
|
||||
valve.setSuffix(".log");
|
||||
factory.addContextValves(valve);
|
||||
factory.addContextValves(this.accesslog.createAccessLogValve());
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -110,18 +110,20 @@ public class ServerPropertiesTests {
|
|||
@Test
|
||||
public void testTomcatBinding() throws Exception {
|
||||
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.remote_ip_header", "Remote-Ip");
|
||||
map.put("server.tomcat.internal_proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
|
||||
bindProperties(map);
|
||||
assertEquals("%h %t '%r' %s %b", this.properties.getTomcat()
|
||||
.getAccessLogPattern());
|
||||
assertEquals("Remote-Ip", this.properties.getTomcat().getRemoteIpHeader());
|
||||
assertEquals("X-Forwarded-Protocol", this.properties.getTomcat()
|
||||
.getProtocolHeader());
|
||||
assertEquals("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", this.properties.getTomcat()
|
||||
.getInternalProxies());
|
||||
ServerProperties.Tomcat tomcat = this.properties.getTomcat();
|
||||
assertEquals("%h %t '%r' %s %b", tomcat.getAccesslog().getPattern());
|
||||
assertEquals("foo", tomcat.getAccesslog().getPrefix());
|
||||
assertEquals("-bar.log", tomcat.getAccesslog().getSuffix());
|
||||
assertEquals("Remote-Ip", tomcat.getRemoteIpHeader());
|
||||
assertEquals("X-Forwarded-Protocol", tomcat.getProtocolHeader());
|
||||
assertEquals("10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}", tomcat.getInternalProxies());
|
||||
}
|
||||
|
||||
@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-provider=
|
||||
server.ssl.trust-store-type=
|
||||
server.tomcat.access-log-pattern= # log pattern of the access log
|
||||
server.tomcat.access-log-enabled=false # is access logging enabled
|
||||
server.tomcat.accesslog.directory=logs # directory in which log files are created
|
||||
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}|\\
|
||||
192\\.168\\.\\d{1,3}\\.\\d{1,3}|\\
|
||||
169\\.254\\.\\d{1,3}\\.\\d{1,3}|\\
|
||||
|
|
|
|||
Loading…
Reference in New Issue