Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f823ae4

Browse files
amcghiesnicoll
authored andcommitted
Complete support for customizing Tomcat's access log
See spring-projectsgh-16039
1 parent 6615e11 commit f823ae4

File tree

4 files changed

+193
-0
lines changed

4 files changed

+193
-0
lines changed

‎spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
* @author Olivier Lamy
5757
* @author Chentao Qu
5858
* @author Artsiom Yudovin
59+
* @author Andrew McGhie
5960
*/
6061
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
6162
public class ServerProperties {
@@ -598,6 +599,42 @@ public static class Accesslog {
598599
*/
599600
private boolean buffered = true;
600601

602+
/**
603+
* Check for log file existence so it can be recreated it if an external
604+
* process/agent has renamed it.
605+
*/
606+
private boolean checkExists = false;
607+
608+
/**
609+
* If the value returned from ServletRequest.getAttribute(conditionIf) yields
610+
* a null value, logging of the request will be skipped.
611+
*/
612+
private String conditionIf;
613+
614+
/**
615+
* If the value returned from ServletRequest.getAttribute(conditionUnless)
616+
* yields a non-null value, the logging of the request will be skipped.
617+
*/
618+
private String conditionUnless;
619+
620+
/**
621+
* Character set used by the log file. If it is <code>null</code>, the system
622+
* default character set will be used. An empty string will be treated as
623+
* <code>null</code> when this property is assigned.
624+
*/
625+
private String encoding;
626+
627+
/**
628+
* Use IPv6 canonical representation format as defined by RFC 5952 in output.
629+
*/
630+
private boolean ipv6Canonical = false;
631+
632+
/**
633+
* Set the locale used to format timestamps in log entries and in log file
634+
* name suffix.
635+
*/
636+
private String locale = Locale.getDefault().toString();
637+
601638
public boolean isEnabled() {
602639
return this.enabled;
603640
}
@@ -686,6 +723,54 @@ public void setBuffered(boolean buffered) {
686723
this.buffered = buffered;
687724
}
688725

726+
public boolean isCheckExists() {
727+
return this.checkExists;
728+
}
729+
730+
public void setCheckExists(boolean checkExists) {
731+
this.checkExists = checkExists;
732+
}
733+
734+
public String getConditionIf() {
735+
return this.conditionIf;
736+
}
737+
738+
public void setConditionIf(String conditionIf) {
739+
this.conditionIf = conditionIf;
740+
}
741+
742+
public String getConditionUnless() {
743+
return this.conditionUnless;
744+
}
745+
746+
public void setConditionUnless(String conditionUnless) {
747+
this.conditionUnless = conditionUnless;
748+
}
749+
750+
public String getEncoding() {
751+
return this.encoding;
752+
}
753+
754+
public void setEncoding(String encoding) {
755+
this.encoding = encoding;
756+
}
757+
758+
public boolean isIpv6Canonical() {
759+
return this.ipv6Canonical;
760+
}
761+
762+
public void setIpv6Canonical(boolean ipv6Canonical) {
763+
this.ipv6Canonical = ipv6Canonical;
764+
}
765+
766+
public String getLocale() {
767+
return this.locale;
768+
}
769+
770+
public void setLocale(String locale) {
771+
this.locale = locale;
772+
}
773+
689774
}
690775

691776
/**

‎spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* @author Phillip Webb
5050
* @author Artsiom Yudovin
5151
* @author Chentao Qu
52+
* @author Andrew McGhie
5253
* @since 2.0.0
5354
*/
5455
public class TomcatWebServerFactoryCustomizer implements
@@ -257,6 +258,12 @@ private void customizeAccessLog(ConfigurableTomcatWebServerFactory factory) {
257258
tomcatProperties.getAccesslog().isRequestAttributesEnabled());
258259
valve.setRotatable(tomcatProperties.getAccesslog().isRotate());
259260
valve.setBuffered(tomcatProperties.getAccesslog().isBuffered());
261+
valve.setCheckExists(tomcatProperties.getAccesslog().isCheckExists());
262+
valve.setConditionIf(tomcatProperties.getAccesslog().getConditionIf());
263+
valve.setConditionUnless(tomcatProperties.getAccesslog().getConditionUnless());
264+
valve.setEncoding(tomcatProperties.getAccesslog().getEncoding());
265+
valve.setIpv6Canonical(tomcatProperties.getAccesslog().isIpv6Canonical());
266+
valve.setLocale(tomcatProperties.getAccesslog().getLocale());
260267
factory.addEngineValves(valve);
261268
}
262269

‎spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
* @author Eddú Meléndez
7373
* @author Quinten De Swaef
7474
* @author Venil Noronha
75+
* @author Andrew McGhie
7576
*/
7677
public class ServerPropertiesTests {
7778

@@ -117,6 +118,12 @@ public void testTomcatBinding() {
117118
map.put("server.tomcat.accesslog.rename-on-rotate", "true");
118119
map.put("server.tomcat.accesslog.request-attributes-enabled", "true");
119120
map.put("server.tomcat.accesslog.suffix", "-bar.log");
121+
map.put("server.tomcat.accesslog.checkExists", "true");
122+
map.put("server.tomcat.accesslog.conditionIf", "foo");
123+
map.put("server.tomcat.accesslog.conditionUnless", "bar");
124+
map.put("server.tomcat.accesslog.encoding", "UTF-8");
125+
map.put("server.tomcat.accesslog.ipv6Canonical", "true");
126+
map.put("server.tomcat.accesslog.locale", "en-AU");
120127
map.put("server.tomcat.protocol-header", "X-Forwarded-Protocol");
121128
map.put("server.tomcat.remote-ip-header", "Remote-Ip");
122129
map.put("server.tomcat.internal-proxies", "10\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
@@ -129,6 +136,12 @@ public void testTomcatBinding() {
129136
assertThat(tomcat.getAccesslog().isRenameOnRotate()).isTrue();
130137
assertThat(tomcat.getAccesslog().isRequestAttributesEnabled()).isTrue();
131138
assertThat(tomcat.getAccesslog().getSuffix()).isEqualTo("-bar.log");
139+
assertThat(tomcat.getAccesslog().isCheckExists()).isEqualTo(true);
140+
assertThat(tomcat.getAccesslog().getConditionIf()).isEqualTo("foo");
141+
assertThat(tomcat.getAccesslog().getConditionUnless()).isEqualTo("bar");
142+
assertThat(tomcat.getAccesslog().getEncoding()).isEqualTo("UTF-8");
143+
assertThat(tomcat.getAccesslog().isIpv6Canonical()).isTrue();
144+
assertThat(tomcat.getAccesslog().getLocale()).isEqualTo("en-AU");
132145
assertThat(tomcat.getRemoteIpHeader()).isEqualTo("Remote-Ip");
133146
assertThat(tomcat.getProtocolHeader()).isEqualTo("X-Forwarded-Protocol");
134147
assertThat(tomcat.getInternalProxies())

‎spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizerTests.java‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.boot.autoconfigure.web.embedded;
1818

19+
import java.util.Locale;
1920
import java.util.function.Consumer;
2021

2122
import org.apache.catalina.Context;
@@ -49,6 +50,7 @@
4950
* @author Rob Tompkins
5051
* @author Artsiom Yudovin
5152
* @author Stephane Nicoll
53+
* @author Andrew McGhie
5254
*/
5355
public class TomcatWebServerFactoryCustomizerTests {
5456

@@ -333,6 +335,92 @@ public void accessLogMaxDaysCanBeRedefined() {
333335
.getMaxDays()).isEqualTo(20);
334336
}
335337

338+
@Test
339+
public void accessLogCheckExistsDefault() {
340+
bind("server.tomcat.accesslog.enabled=true");
341+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
342+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
343+
.isCheckExists()).isFalse();
344+
}
345+
346+
@Test
347+
public void accessLogCheckExistsSpecified() {
348+
bind("server.tomcat.accesslog.enabled=true",
349+
"server.tomcat.accesslog.checkExists=true");
350+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
351+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
352+
.isCheckExists()).isTrue();
353+
}
354+
355+
@Test
356+
public void accessLogConditionCanBeSpecified() {
357+
bind("server.tomcat.accesslog.enabled=true",
358+
"server.tomcat.accesslog.conditionIf=foo",
359+
"server.tomcat.accesslog.conditionUnless=bar");
360+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
361+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
362+
.getConditionIf()).isEqualTo("foo");
363+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
364+
.getConditionUnless()).isEqualTo("bar");
365+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
366+
.getCondition()).describedAs(
367+
"value of condition should equal conditionUnless - provided for backwards compatibility")
368+
.isEqualTo("bar");
369+
}
370+
371+
@Test
372+
public void accessLogEncodingIsNullWhenNotSpecified() {
373+
bind("server.tomcat.accesslog.enabled=true");
374+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
375+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
376+
.getEncoding()).isNull();
377+
}
378+
379+
@Test
380+
public void accessLogEncodingCanBeSpecified() {
381+
bind("server.tomcat.accesslog.enabled=true",
382+
"server.tomcat.accesslog.encoding=UTF-8");
383+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
384+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
385+
.getEncoding()).isEqualTo("UTF-8");
386+
}
387+
388+
@Test
389+
public void accessLogDoesNotUseIpv6CanonicalFormatByDefault() {
390+
bind("server.tomcat.accesslog.enabled=true");
391+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
392+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
393+
.getIpv6Canonical()).isFalse();
394+
}
395+
396+
@Test
397+
public void accessLogwithIpv6CanonicalSet() {
398+
bind("server.tomcat.accesslog.enabled=true",
399+
"server.tomcat.accesslog.ipv6Canonical=true");
400+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
401+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
402+
.getIpv6Canonical()).isTrue();
403+
}
404+
405+
@Test
406+
public void accessLogWithDefaultLocale() {
407+
bind("server.tomcat.accesslog.enabled=true");
408+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
409+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
410+
.getLocale()).isEqualTo(Locale.getDefault().toString());
411+
}
412+
413+
@Test
414+
public void accessLogLocaleCanBeSpecified() {
415+
String locale = "en_AU".equals(Locale.getDefault().toString()) ? "en_US"
416+
: "en_AU";
417+
bind("server.tomcat.accesslog.enabled=true",
418+
"server.tomcat.accesslog.locale=" + locale);
419+
TomcatServletWebServerFactory factory = customizeAndGetFactory();
420+
assertThat(((AccessLogValve) factory.getEngineValves().iterator().next())
421+
.getLocale()).isEqualTo(locale);
422+
}
423+
336424
private void bind(String... inlinedProperties) {
337425
TestPropertySourceUtils.addInlinedPropertiesToEnvironment(this.environment,
338426
inlinedProperties);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /