Polish "Complete Jetty Access Log configuration properties support"

Closes gh-16080
pull/15988/head
Stephane Nicoll 6 years ago
parent 55a5a26b95
commit 0f60555031

@ -949,14 +949,15 @@ public class ServerProperties {
private boolean logLatency;
/**
* Set request paths that will not be logged.
* Whether to log IP address from the "X-Forwarded-For" header rather than the
* one from the connection.
*/
private List<String> ignorePaths;
private boolean preferProxiedForAddress = false;
/**
* true - IP address from header will be logged, false - IP address from the connection will be logged
* Request paths that should not be logged.
*/
private boolean preferProxiedForAddress = false;
private List<String> ignorePaths;
public boolean isEnabled() {
return this.enabled;
@ -1054,21 +1055,22 @@ public class ServerProperties {
this.logLatency = logLatency;
}
public List<String> getIgnorePaths() {
return ignorePaths;
public boolean isPreferProxiedForAddress() {
return this.preferProxiedForAddress;
}
public void setIgnorePaths(List<String> ignorePaths) {
this.ignorePaths = ignorePaths;
public void setPreferProxiedForAddress(boolean preferProxiedForAddress) {
this.preferProxiedForAddress = preferProxiedForAddress;
}
public boolean getPreferProxiedForAddress(){
return preferProxiedForAddress;
public List<String> getIgnorePaths() {
return this.ignorePaths;
}
public void setPreferProxiedForAddress(boolean preferProxiedForAddress){
this.preferProxiedForAddress = preferProxiedForAddress;
public void setIgnorePaths(List<String> ignorePaths) {
this.ignorePaths = ignorePaths;
}
}
}

@ -37,6 +37,7 @@ import org.springframework.boot.web.embedded.jetty.JettyServerCustomizer;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.util.CollectionUtils;
import org.springframework.util.unit.DataSize;
/**
@ -167,13 +168,13 @@ public class JettyWebServerFactoryCustomizer implements
if (properties.getTimeZone() != null) {
log.setLogTimeZone(properties.getTimeZone().getID());
}
if (properties.getIgnorePaths() != null) {
log.setIgnorePaths(properties.getIgnorePaths().toArray(new String[0]));
}
log.setLogCookies(properties.isLogCookies());
log.setLogServer(properties.isLogServer());
log.setLogLatency(properties.isLogLatency());
log.setPreferProxiedForAddress(properties.getPreferProxiedForAddress());
log.setPreferProxiedForAddress(properties.isPreferProxiedForAddress());
if (!CollectionUtils.isEmpty(properties.getIgnorePaths())) {
log.setIgnorePaths(properties.getIgnorePaths().toArray(new String[0]));
}
server.setRequestLog(log);
});
}

@ -231,8 +231,8 @@ public class ServerPropertiesTests {
map.put("server.jetty.accesslog.file-date-format", "yyyymmdd");
map.put("server.jetty.accesslog.retention-period", "4");
map.put("server.jetty.accesslog.append", "true");
map.put("server.jetty.accesslog.ignore-paths[0]", "/a/path");
map.put("server.jetty.accesslog.ignore-paths[1]", "/b/path");
map.put("server.jetty.accesslog.prefer-proxied-for-address", "true");
map.put("server.jetty.accesslog.ignore-paths", "/a/path,/b/path");
bind(map);
ServerProperties.Jetty jetty = this.properties.getJetty();
assertThat(jetty.getAccesslog().isEnabled()).isTrue();
@ -240,9 +240,9 @@ public class ServerPropertiesTests {
assertThat(jetty.getAccesslog().getFileDateFormat()).isEqualTo("yyyymmdd");
assertThat(jetty.getAccesslog().getRetentionPeriod()).isEqualTo(4);
assertThat(jetty.getAccesslog().isAppend()).isTrue();
assertThat(jetty.getAccesslog().getIgnorePaths().size()).isEqualTo(2);
assertThat(jetty.getAccesslog().getIgnorePaths().get(0)).isEqualTo("/a/path");
assertThat(jetty.getAccesslog().getIgnorePaths().get(1)).isEqualTo("/b/path");
assertThat(jetty.getAccesslog().isPreferProxiedForAddress()).isTrue();
assertThat(jetty.getAccesslog().getIgnorePaths()).containsExactly("/a/path",
"/b/path");
}
@Test

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,16 +16,13 @@
package org.springframework.boot.autoconfigure.web.embedded;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.HttpConfiguration;
import org.eclipse.jetty.server.HttpConfiguration.ConnectionFactory;
@ -33,6 +30,7 @@ import org.eclipse.jetty.server.NCSARequestLog;
import org.eclipse.jetty.server.RequestLog;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
@ -43,6 +41,10 @@ import org.springframework.boot.web.embedded.jetty.JettyWebServer;
import org.springframework.mock.env.MockEnvironment;
import org.springframework.test.context.support.TestPropertySourceUtils;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
* Tests for {@link JettyWebServerFactoryCustomizer}.
*
@ -101,8 +103,7 @@ public class JettyWebServerFactoryCustomizerTests {
"server.jetty.accesslog.log-server=true",
"server.jetty.accesslog.log-latency=true",
"server.jetty.accesslog.prefer-proxied-for-address=true",
"server.jetty.accesslog.ignore-paths[0]=/a/path",
"server.jetty.accesslog.ignore-paths[1]=/b/path");
"server.jetty.accesslog.ignore-paths=/a/path,/b/path");
JettyWebServer server = customizeAndGetServer();
NCSARequestLog requestLog = getNCSARequestLog(server);
assertThat(requestLog.getFilename()).isEqualTo(logFile.getAbsolutePath());
@ -118,8 +119,7 @@ public class JettyWebServerFactoryCustomizerTests {
assertThat(requestLog.getLogLatency()).isTrue();
assertThat(requestLog.getPreferProxiedForAddress()).isTrue();
assertThat(requestLog.getIgnorePaths().length).isEqualTo(2);
assertThat(requestLog.getIgnorePaths()[0]).isEqualTo("/a/path");
assertThat(requestLog.getIgnorePaths()[1]).isEqualTo("/b/path");
assertThat(requestLog.getIgnorePaths()).containsExactly("/a/path", "/b/path");
}
@Test
@ -133,8 +133,8 @@ public class JettyWebServerFactoryCustomizerTests {
assertThat(requestLog.getLogCookies()).isFalse();
assertThat(requestLog.getLogServer()).isFalse();
assertThat(requestLog.getLogLatency()).isFalse();
assertThat(requestLog.getIgnorePaths().length).isZero();
assertThat(requestLog.getPreferProxiedForAddress()).isFalse();
assertThat(requestLog.getIgnorePaths()).isNull();
}
private NCSARequestLog getNCSARequestLog(JettyWebServer server) {

Loading…
Cancel
Save