Skip to content

Commit e9083d7

Browse files
committed
Apply LogFormatUtils in more places
1 parent 98ce171 commit e9083d7

File tree

10 files changed

+56
-35
lines changed

10 files changed

+56
-35
lines changed

spring-web/src/main/java/org/springframework/web/util/UrlPathHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,8 +578,8 @@ private String decodeInternal(HttpServletRequest request, String source) {
578578
return UriUtils.decode(source, enc);
579579
}
580580
catch (UnsupportedCharsetException ex) {
581-
if (logger.isWarnEnabled()) {
582-
logger.warn("Could not decode request string [" + source + "] with encoding '" + enc +
581+
if (logger.isDebugEnabled()) {
582+
logger.debug("Could not decode request string [" + source + "] with encoding '" + enc +
583583
"': falling back to platform default encoding; exception message: " + ex.getMessage());
584584
}
585585
return URLDecoder.decode(source);

spring-webflux/src/main/java/org/springframework/web/reactive/resource/PathResourceResolver.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.core.io.ClassPathResource;
3030
import org.springframework.core.io.Resource;
3131
import org.springframework.core.io.UrlResource;
32+
import org.springframework.core.log.LogFormatUtils;
3233
import org.springframework.lang.Nullable;
3334
import org.springframework.util.StringUtils;
3435
import org.springframework.web.server.ServerWebExchange;
@@ -119,11 +120,12 @@ protected Mono<Resource> getResource(String resourcePath, Resource location) {
119120
return Mono.just(resource);
120121
}
121122
else if (logger.isWarnEnabled()) {
122-
Resource[] allowedLocations = getAllowedLocations();
123-
logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " +
124-
"but resource \"" + resource.getURL() + "\" is neither under the " +
125-
"current location \"" + location.getURL() + "\" nor under any of the " +
126-
"allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]"));
123+
Resource[] allowed = getAllowedLocations();
124+
logger.warn(LogFormatUtils.formatValue(
125+
"Resource path \"" + resourcePath + "\" was successfully resolved " +
126+
"but resource \"" + resource.getURL() + "\" is neither under the " +
127+
"current location \"" + location.getURL() + "\" nor under any of the " +
128+
"allowed locations " + (allowed != null ? Arrays.asList(allowed) : "[]"), -1, true));
127129
}
128130
}
129131
return Mono.empty();
@@ -199,7 +201,8 @@ private boolean isInvalidEncodedPath(String resourcePath) {
199201
try {
200202
String decodedPath = URLDecoder.decode(resourcePath, "UTF-8");
201203
if (decodedPath.contains("../") || decodedPath.contains("..\\")) {
202-
logger.warn("Resolved resource path contains encoded \"../\" or \"..\\\": " + resourcePath);
204+
logger.warn(LogFormatUtils.formatValue(
205+
"Resolved resource path contains encoded \"../\" or \"..\\\": " + resourcePath, -1, true));
203206
return true;
204207
}
205208
}

spring-webflux/src/main/java/org/springframework/web/reactive/resource/ResourceWebHandler.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.core.codec.Hints;
4040
import org.springframework.core.io.Resource;
4141
import org.springframework.core.io.ResourceLoader;
42+
import org.springframework.core.log.LogFormatUtils;
4243
import org.springframework.http.CacheControl;
4344
import org.springframework.http.HttpHeaders;
4445
import org.springframework.http.HttpMethod;
@@ -572,22 +573,25 @@ private boolean isInvalidEncodedPath(String path) {
572573
protected boolean isInvalidPath(String path) {
573574
if (path.contains("WEB-INF") || path.contains("META-INF")) {
574575
if (logger.isWarnEnabled()) {
575-
logger.warn("Path with \"WEB-INF\" or \"META-INF\": [" + path + "]");
576+
logger.warn(LogFormatUtils.formatValue(
577+
"Path with \"WEB-INF\" or \"META-INF\": [" + path + "]", -1, true));
576578
}
577579
return true;
578580
}
579581
if (path.contains(":/")) {
580582
String relativePath = (path.charAt(0) == '/' ? path.substring(1) : path);
581583
if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) {
582584
if (logger.isWarnEnabled()) {
583-
logger.warn("Path represents URL or has \"url:\" prefix: [" + path + "]");
585+
logger.warn(LogFormatUtils.formatValue(
586+
"Path represents URL or has \"url:\" prefix: [" + path + "]", -1, true));
584587
}
585588
return true;
586589
}
587590
}
588591
if (path.contains("..") && StringUtils.cleanPath(path).contains("../")) {
589592
if (logger.isWarnEnabled()) {
590-
logger.warn("Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]");
593+
logger.warn(LogFormatUtils.formatValue(
594+
"Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]", -1, true));
591595
}
592596
return true;
593597
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ExtendedServletRequestDataBinder.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -69,9 +69,8 @@ protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request)
6969
if (uriVars != null) {
7070
uriVars.forEach((name, value) -> {
7171
if (mpvs.contains(name)) {
72-
if (logger.isWarnEnabled()) {
73-
logger.warn("Skipping URI variable '" + name +
74-
"' because request contains bind value with same name.");
72+
if (logger.isDebugEnabled()) {
73+
logger.debug("URI variable '" + name + "' overridden by request bind value.");
7574
}
7675
}
7776
else {

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ReactiveTypeHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -202,7 +202,7 @@ private void logExecutorWarning(MethodParameter returnType) {
202202
"-------------------------------\n" +
203203
"Controller:\t" + returnType.getContainingClass().getName() + "\n" +
204204
"Method:\t\t" + returnType.getMethod().getName() + "\n" +
205-
"Returning:\t" + ResolvableType.forMethodParameter(returnType).toString() + "\n" +
205+
"Returning:\t" + ResolvableType.forMethodParameter(returnType) + "\n" +
206206
"!!!");
207207
this.taskExecutorWarning = false;
208208
}

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/PathResourceResolver.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.springframework.core.io.ClassPathResource;
3434
import org.springframework.core.io.Resource;
3535
import org.springframework.core.io.UrlResource;
36+
import org.springframework.core.log.LogFormatUtils;
3637
import org.springframework.http.server.PathContainer;
3738
import org.springframework.lang.Nullable;
3839
import org.springframework.util.StringUtils;
@@ -190,11 +191,12 @@ protected Resource getResource(String resourcePath, Resource location) throws IO
190191
return resource;
191192
}
192193
else if (logger.isWarnEnabled()) {
193-
Resource[] allowedLocations = getAllowedLocations();
194-
logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " +
195-
"but resource \"" + resource.getURL() + "\" is neither under the " +
196-
"current location \"" + location.getURL() + "\" nor under any of the " +
197-
"allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]"));
194+
Resource[] allowed = getAllowedLocations();
195+
logger.warn(LogFormatUtils.formatValue(
196+
"Resource path \"" + resourcePath + "\" was successfully resolved " +
197+
"but resource \"" + resource.getURL() + "\" is neither under " +
198+
"the current location \"" + location.getURL() + "\" nor under any of " +
199+
"the allowed locations " + (allowed != null ? Arrays.asList(allowed) : "[]"), -1, true));
198200
}
199201
}
200202
return null;
@@ -297,7 +299,8 @@ private boolean isInvalidEncodedPath(String resourcePath) {
297299
try {
298300
String decodedPath = URLDecoder.decode(resourcePath, "UTF-8");
299301
if (decodedPath.contains("../") || decodedPath.contains("..\\")) {
300-
logger.warn("Resolved resource path contains encoded \"../\" or \"..\\\": " + resourcePath);
302+
logger.warn(LogFormatUtils.formatValue(
303+
"Resolved resource path contains encoded \"../\" or \"..\\\": " + resourcePath, -1, true));
301304
return true;
302305
}
303306
}

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceHttpRequestHandler.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.springframework.context.EmbeddedValueResolverAware;
4040
import org.springframework.core.io.Resource;
4141
import org.springframework.core.io.UrlResource;
42+
import org.springframework.core.log.LogFormatUtils;
4243
import org.springframework.http.HttpHeaders;
4344
import org.springframework.http.HttpMethod;
4445
import org.springframework.http.HttpRange;
@@ -734,22 +735,25 @@ private boolean isInvalidEncodedPath(String path) {
734735
protected boolean isInvalidPath(String path) {
735736
if (path.contains("WEB-INF") || path.contains("META-INF")) {
736737
if (logger.isWarnEnabled()) {
737-
logger.warn("Path with \"WEB-INF\" or \"META-INF\": [" + path + "]");
738+
logger.warn(LogFormatUtils.formatValue(
739+
"Path with \"WEB-INF\" or \"META-INF\": [" + path + "]", -1, true));
738740
}
739741
return true;
740742
}
741743
if (path.contains(":/")) {
742744
String relativePath = (path.charAt(0) == '/' ? path.substring(1) : path);
743745
if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) {
744746
if (logger.isWarnEnabled()) {
745-
logger.warn("Path represents URL or has \"url:\" prefix: [" + path + "]");
747+
logger.warn(LogFormatUtils.formatValue(
748+
"Path represents URL or has \"url:\" prefix: [" + path + "]", -1, true));
746749
}
747750
return true;
748751
}
749752
}
750753
if (path.contains("..") && StringUtils.cleanPath(path).contains("../")) {
751754
if (logger.isWarnEnabled()) {
752-
logger.warn("Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]");
755+
logger.warn(LogFormatUtils.formatValue(
756+
"Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]", -1, true));
753757
}
754758
return true;
755759
}

spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.commons.logging.LogFactory;
3030

3131
import org.springframework.context.Lifecycle;
32+
import org.springframework.core.log.LogFormatUtils;
3233
import org.springframework.http.HttpMethod;
3334
import org.springframework.http.HttpStatus;
3435
import org.springframework.http.server.ServerHttpRequest;
@@ -299,15 +300,17 @@ public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse r
299300

300301
protected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
301302
if (logger.isErrorEnabled()) {
302-
logger.error("Handshake failed due to invalid Upgrade header: " + request.getHeaders().getUpgrade());
303+
logger.error(LogFormatUtils.formatValue(
304+
"Handshake failed due to invalid Upgrade header: " + request.getHeaders().getUpgrade(), -1, true));
303305
}
304306
response.setStatusCode(HttpStatus.BAD_REQUEST);
305307
response.getBody().write("Can \"Upgrade\" only to \"WebSocket\".".getBytes(StandardCharsets.UTF_8));
306308
}
307309

308310
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
309311
if (logger.isErrorEnabled()) {
310-
logger.error("Handshake failed due to invalid Connection header " + request.getHeaders().getConnection());
312+
logger.error(LogFormatUtils.formatValue(
313+
"Handshake failed due to invalid Connection header" + request.getHeaders().getConnection(), -1, true));
311314
}
312315
response.setStatusCode(HttpStatus.BAD_REQUEST);
313316
response.getBody().write("\"Connection\" must be \"upgrade\".".getBytes(StandardCharsets.UTF_8));
@@ -331,8 +334,9 @@ protected String[] getSupportedVersions() {
331334
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) {
332335
if (logger.isErrorEnabled()) {
333336
String version = request.getHeaders().getFirst("Sec-WebSocket-Version");
334-
logger.error("Handshake failed due to unsupported WebSocket version: " + version +
335-
". Supported versions: " + Arrays.toString(getSupportedVersions()));
337+
logger.error(LogFormatUtils.formatValue(
338+
"Handshake failed due to unsupported WebSocket version: " + version +
339+
". Supported versions: " + Arrays.toString(getSupportedVersions()), -1, true));
336340
}
337341
response.setStatusCode(HttpStatus.UPGRADE_REQUIRED);
338342
response.getHeaders().set(WebSocketHttpHeaders.SEC_WEBSOCKET_VERSION,

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/support/AbstractSockJsService.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.commons.logging.Log;
3333
import org.apache.commons.logging.LogFactory;
3434

35+
import org.springframework.core.log.LogFormatUtils;
3536
import org.springframework.http.HttpHeaders;
3637
import org.springframework.http.HttpMethod;
3738
import org.springframework.http.HttpStatus;
@@ -377,7 +378,8 @@ public final void handleRequest(ServerHttpRequest request, ServerHttpResponse re
377378

378379
if (sockJsPath == null) {
379380
if (logger.isWarnEnabled()) {
380-
logger.warn("Expected SockJS path. Failing request: " + request.getURI());
381+
logger.warn(LogFormatUtils.formatValue(
382+
"Expected SockJS path. Failing request: " + request.getURI(), -1, true));
381383
}
382384
response.setStatusCode(HttpStatus.NOT_FOUND);
383385
return;
@@ -447,7 +449,8 @@ else if (requestInfo != null) {
447449
String[] pathSegments = StringUtils.tokenizeToStringArray(sockJsPath.substring(1), "/");
448450
if (pathSegments.length != 3) {
449451
if (logger.isWarnEnabled()) {
450-
logger.warn("Invalid SockJS path '" + sockJsPath + "' - required to have 3 path segments");
452+
logger.warn(LogFormatUtils.formatValue("Invalid SockJS path '" + sockJsPath + "' - " +
453+
"required to have 3 path segments", -1, true));
451454
}
452455
if (requestInfo != null) {
453456
logger.debug("Ignoring transport request: " + requestInfo);

spring-websocket/src/main/java/org/springframework/web/socket/sockjs/transport/TransportHandlingSockJsService.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -30,6 +30,7 @@
3030
import java.util.concurrent.ScheduledFuture;
3131

3232
import org.springframework.context.Lifecycle;
33+
import org.springframework.core.log.LogFormatUtils;
3334
import org.springframework.http.HttpMethod;
3435
import org.springframework.http.HttpStatus;
3536
import org.springframework.http.server.ServerHttpRequest;
@@ -234,7 +235,7 @@ protected void handleTransportRequest(ServerHttpRequest request, ServerHttpRespo
234235
TransportType transportType = TransportType.fromValue(transport);
235236
if (transportType == null) {
236237
if (logger.isWarnEnabled()) {
237-
logger.warn("Unknown transport type for " + request.getURI());
238+
logger.warn(LogFormatUtils.formatValue("Unknown transport type for " + request.getURI(), -1, true));
238239
}
239240
response.setStatusCode(HttpStatus.NOT_FOUND);
240241
return;
@@ -243,7 +244,7 @@ protected void handleTransportRequest(ServerHttpRequest request, ServerHttpRespo
243244
TransportHandler transportHandler = this.handlers.get(transportType);
244245
if (transportHandler == null) {
245246
if (logger.isWarnEnabled()) {
246-
logger.warn("No TransportHandler for " + request.getURI());
247+
logger.warn(LogFormatUtils.formatValue("No TransportHandler for " + request.getURI(), -1, true));
247248
}
248249
response.setStatusCode(HttpStatus.NOT_FOUND);
249250
return;

0 commit comments

Comments
 (0)