Skip to content

Commit 0f60555

Browse files
committed
Polish "Complete Jetty Access Log configuration properties support"
Closes gh-16080
1 parent 55a5a26 commit 0f60555

File tree

4 files changed

+34
-31
lines changed

4 files changed

+34
-31
lines changed

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

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -949,14 +949,15 @@ public static class Accesslog {
949949
private boolean logLatency;
950950

951951
/**
952-
* Set request paths that will not be logged.
952+
* Whether to log IP address from the "X-Forwarded-For" header rather than the
953+
* one from the connection.
953954
*/
954-
private List<String> ignorePaths;
955+
private boolean preferProxiedForAddress = false;
955956

956957
/**
957-
* true - IP address from header will be logged, false - IP address from the connection will be logged
958+
* Request paths that should not be logged.
958959
*/
959-
private boolean preferProxiedForAddress = false;
960+
private List<String> ignorePaths;
960961

961962
public boolean isEnabled() {
962963
return this.enabled;
@@ -1054,21 +1055,22 @@ public void setLogLatency(boolean logLatency) {
10541055
this.logLatency = logLatency;
10551056
}
10561057

1057-
public List<String> getIgnorePaths() {
1058-
return ignorePaths;
1058+
public boolean isPreferProxiedForAddress() {
1059+
return this.preferProxiedForAddress;
10591060
}
10601061

1061-
public void setIgnorePaths(List<String> ignorePaths) {
1062-
this.ignorePaths = ignorePaths;
1062+
public void setPreferProxiedForAddress(boolean preferProxiedForAddress) {
1063+
this.preferProxiedForAddress = preferProxiedForAddress;
10631064
}
10641065

1065-
public boolean getPreferProxiedForAddress(){
1066-
return preferProxiedForAddress;
1066+
public List<String> getIgnorePaths() {
1067+
return this.ignorePaths;
10671068
}
10681069

1069-
public void setPreferProxiedForAddress(boolean preferProxiedForAddress){
1070-
this.preferProxiedForAddress = preferProxiedForAddress;
1070+
public void setIgnorePaths(List<String> ignorePaths) {
1071+
this.ignorePaths = ignorePaths;
10711072
}
1073+
10721074
}
10731075

10741076
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
3838
import org.springframework.core.Ordered;
3939
import org.springframework.core.env.Environment;
40+
import org.springframework.util.CollectionUtils;
4041
import org.springframework.util.unit.DataSize;
4142

4243
/**
@@ -167,13 +168,13 @@ private void customizeAccessLog(ConfigurableJettyWebServerFactory factory,
167168
if (properties.getTimeZone() != null) {
168169
log.setLogTimeZone(properties.getTimeZone().getID());
169170
}
170-
if (properties.getIgnorePaths() != null) {
171-
log.setIgnorePaths(properties.getIgnorePaths().toArray(new String[0]));
172-
}
173171
log.setLogCookies(properties.isLogCookies());
174172
log.setLogServer(properties.isLogServer());
175173
log.setLogLatency(properties.isLogLatency());
176-
log.setPreferProxiedForAddress(properties.getPreferProxiedForAddress());
174+
log.setPreferProxiedForAddress(properties.isPreferProxiedForAddress());
175+
if (!CollectionUtils.isEmpty(properties.getIgnorePaths())) {
176+
log.setIgnorePaths(properties.getIgnorePaths().toArray(new String[0]));
177+
}
177178
server.setRequestLog(log);
178179
});
179180
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,18 +231,18 @@ public void testCustomizeJettyAccessLog() {
231231
map.put("server.jetty.accesslog.file-date-format", "yyyymmdd");
232232
map.put("server.jetty.accesslog.retention-period", "4");
233233
map.put("server.jetty.accesslog.append", "true");
234-
map.put("server.jetty.accesslog.ignore-paths[0]", "/a/path");
235-
map.put("server.jetty.accesslog.ignore-paths[1]", "/b/path");
234+
map.put("server.jetty.accesslog.prefer-proxied-for-address", "true");
235+
map.put("server.jetty.accesslog.ignore-paths", "/a/path,/b/path");
236236
bind(map);
237237
ServerProperties.Jetty jetty = this.properties.getJetty();
238238
assertThat(jetty.getAccesslog().isEnabled()).isTrue();
239239
assertThat(jetty.getAccesslog().getFilename()).isEqualTo("foo.txt");
240240
assertThat(jetty.getAccesslog().getFileDateFormat()).isEqualTo("yyyymmdd");
241241
assertThat(jetty.getAccesslog().getRetentionPeriod()).isEqualTo(4);
242242
assertThat(jetty.getAccesslog().isAppend()).isTrue();
243-
assertThat(jetty.getAccesslog().getIgnorePaths().size()).isEqualTo(2);
244-
assertThat(jetty.getAccesslog().getIgnorePaths().get(0)).isEqualTo("/a/path");
245-
assertThat(jetty.getAccesslog().getIgnorePaths().get(1)).isEqualTo("/b/path");
243+
assertThat(jetty.getAccesslog().isPreferProxiedForAddress()).isTrue();
244+
assertThat(jetty.getAccesslog().getIgnorePaths()).containsExactly("/a/path",
245+
"/b/path");
246246
}
247247

248248
@Test

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,23 +16,21 @@
1616

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

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
import static org.mockito.Mockito.mock;
21-
import static org.mockito.Mockito.verify;
22-
2319
import java.io.File;
2420
import java.io.IOException;
2521
import java.util.ArrayList;
2622
import java.util.List;
2723
import java.util.Locale;
2824
import java.util.TimeZone;
25+
2926
import org.eclipse.jetty.server.Connector;
3027
import org.eclipse.jetty.server.HttpConfiguration;
3128
import org.eclipse.jetty.server.HttpConfiguration.ConnectionFactory;
3229
import org.eclipse.jetty.server.NCSARequestLog;
3330
import org.eclipse.jetty.server.RequestLog;
3431
import org.junit.Before;
3532
import org.junit.Test;
33+
3634
import org.springframework.boot.autoconfigure.web.ServerProperties;
3735
import org.springframework.boot.context.properties.bind.Bindable;
3836
import org.springframework.boot.context.properties.bind.Binder;
@@ -43,6 +41,10 @@
4341
import org.springframework.mock.env.MockEnvironment;
4442
import org.springframework.test.context.support.TestPropertySourceUtils;
4543

44+
import static org.assertj.core.api.Assertions.assertThat;
45+
import static org.mockito.Mockito.mock;
46+
import static org.mockito.Mockito.verify;
47+
4648
/**
4749
* Tests for {@link JettyWebServerFactoryCustomizer}.
4850
*
@@ -101,8 +103,7 @@ public void accessLogCanBeCustomized() throws IOException {
101103
"server.jetty.accesslog.log-server=true",
102104
"server.jetty.accesslog.log-latency=true",
103105
"server.jetty.accesslog.prefer-proxied-for-address=true",
104-
"server.jetty.accesslog.ignore-paths[0]=/a/path",
105-
"server.jetty.accesslog.ignore-paths[1]=/b/path");
106+
"server.jetty.accesslog.ignore-paths=/a/path,/b/path");
106107
JettyWebServer server = customizeAndGetServer();
107108
NCSARequestLog requestLog = getNCSARequestLog(server);
108109
assertThat(requestLog.getFilename()).isEqualTo(logFile.getAbsolutePath());
@@ -118,8 +119,7 @@ public void accessLogCanBeCustomized() throws IOException {
118119
assertThat(requestLog.getLogLatency()).isTrue();
119120
assertThat(requestLog.getPreferProxiedForAddress()).isTrue();
120121
assertThat(requestLog.getIgnorePaths().length).isEqualTo(2);
121-
assertThat(requestLog.getIgnorePaths()[0]).isEqualTo("/a/path");
122-
assertThat(requestLog.getIgnorePaths()[1]).isEqualTo("/b/path");
122+
assertThat(requestLog.getIgnorePaths()).containsExactly("/a/path", "/b/path");
123123
}
124124

125125
@Test
@@ -133,8 +133,8 @@ public void accessLogCanBeEnabled() {
133133
assertThat(requestLog.getLogCookies()).isFalse();
134134
assertThat(requestLog.getLogServer()).isFalse();
135135
assertThat(requestLog.getLogLatency()).isFalse();
136-
assertThat(requestLog.getIgnorePaths().length).isZero();
137136
assertThat(requestLog.getPreferProxiedForAddress()).isFalse();
137+
assertThat(requestLog.getIgnorePaths()).isNull();
138138
}
139139

140140
private NCSARequestLog getNCSARequestLog(JettyWebServer server) {

0 commit comments

Comments
 (0)