Skip to content

Commit 96a7fc6

Browse files
committed
Apply LogFormatUtils in more places
1 parent 50f6db2 commit 96a7fc6

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
@@ -521,8 +521,8 @@ private String decodeInternal(HttpServletRequest request, String source) {
521521
return UriUtils.decode(source, enc);
522522
}
523523
catch (UnsupportedCharsetException ex) {
524-
if (logger.isWarnEnabled()) {
525-
logger.warn("Could not decode request string [" + source + "] with encoding '" + enc +
524+
if (logger.isDebugEnabled()) {
525+
logger.debug("Could not decode request string [" + source + "] with encoding '" + enc +
526526
"': falling back to platform default encoding; exception message: " + ex.getMessage());
527527
}
528528
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
@@ -36,6 +36,7 @@
3636
import org.springframework.core.codec.Hints;
3737
import org.springframework.core.io.Resource;
3838
import org.springframework.core.io.ResourceLoader;
39+
import org.springframework.core.log.LogFormatUtils;
3940
import org.springframework.http.CacheControl;
4041
import org.springframework.http.HttpHeaders;
4142
import org.springframework.http.HttpMethod;
@@ -490,22 +491,25 @@ private boolean isInvalidEncodedPath(String path) {
490491
protected boolean isInvalidPath(String path) {
491492
if (path.contains("WEB-INF") || path.contains("META-INF")) {
492493
if (logger.isWarnEnabled()) {
493-
logger.warn("Path with \"WEB-INF\" or \"META-INF\": [" + path + "]");
494+
logger.warn(LogFormatUtils.formatValue(
495+
"Path with \"WEB-INF\" or \"META-INF\": [" + path + "]", -1, true));
494496
}
495497
return true;
496498
}
497499
if (path.contains(":/")) {
498500
String relativePath = (path.charAt(0) == '/' ? path.substring(1) : path);
499501
if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) {
500502
if (logger.isWarnEnabled()) {
501-
logger.warn("Path represents URL or has \"url:\" prefix: [" + path + "]");
503+
logger.warn(LogFormatUtils.formatValue(
504+
"Path represents URL or has \"url:\" prefix: [" + path + "]", -1, true));
502505
}
503506
return true;
504507
}
505508
}
506509
if (path.contains("..") && StringUtils.cleanPath(path).contains("../")) {
507510
if (logger.isWarnEnabled()) {
508-
logger.warn("Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]");
511+
logger.warn(LogFormatUtils.formatValue(
512+
"Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]", -1, true));
509513
}
510514
return true;
511515
}

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-2017 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.
@@ -67,9 +67,8 @@ protected void addBindValues(MutablePropertyValues mpvs, ServletRequest request)
6767
if (uriVars != null) {
6868
uriVars.forEach((name, value) -> {
6969
if (mpvs.contains(name)) {
70-
if (logger.isWarnEnabled()) {
71-
logger.warn("Skipping URI variable '" + name +
72-
"' because request contains bind value with same name.");
70+
if (logger.isDebugEnabled()) {
71+
logger.debug("URI variable '" + name + "' overridden by request bind value.");
7372
}
7473
}
7574
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-2019 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.
@@ -193,7 +193,7 @@ private void logExecutorWarning(MethodParameter returnType) {
193193
"-------------------------------\n" +
194194
"Controller:\t" + returnType.getContainingClass().getName() + "\n" +
195195
"Method:\t\t" + returnType.getMethod().getName() + "\n" +
196-
"Returning:\t" + ResolvableType.forMethodParameter(returnType).toString() + "\n" +
196+
"Returning:\t" + ResolvableType.forMethodParameter(returnType) + "\n" +
197197
"!!!");
198198
this.taskExecutorWarning = false;
199199
}

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.lang.Nullable;
3738
import org.springframework.util.StringUtils;
3839
import org.springframework.web.context.support.ServletContextResource;
@@ -188,11 +189,12 @@ protected Resource getResource(String resourcePath, Resource location) throws IO
188189
return resource;
189190
}
190191
else if (logger.isWarnEnabled()) {
191-
Resource[] allowedLocations = getAllowedLocations();
192-
logger.warn("Resource path \"" + resourcePath + "\" was successfully resolved " +
193-
"but resource \"" + resource.getURL() + "\" is neither under the " +
194-
"current location \"" + location.getURL() + "\" nor under any of the " +
195-
"allowed locations " + (allowedLocations != null ? Arrays.asList(allowedLocations) : "[]"));
192+
Resource[] allowed = getAllowedLocations();
193+
logger.warn(LogFormatUtils.formatValue(
194+
"Resource path \"" + resourcePath + "\" was successfully resolved " +
195+
"but resource \"" + resource.getURL() + "\" is neither under " +
196+
"the current location \"" + location.getURL() + "\" nor under any of " +
197+
"the allowed locations " + (allowed != null ? Arrays.asList(allowed) : "[]"), -1, true));
196198
}
197199
}
198200
return null;
@@ -285,7 +287,8 @@ private boolean isInvalidEncodedPath(String resourcePath) {
285287
try {
286288
String decodedPath = URLDecoder.decode(resourcePath, "UTF-8");
287289
if (decodedPath.contains("../") || decodedPath.contains("..\\")) {
288-
logger.warn("Resolved resource path contains encoded \"../\" or \"..\\\": " + resourcePath);
290+
logger.warn(LogFormatUtils.formatValue(
291+
"Resolved resource path contains encoded \"../\" or \"..\\\": " + resourcePath, -1, true));
289292
return true;
290293
}
291294
}

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
@@ -40,6 +40,7 @@
4040
import org.springframework.context.EmbeddedValueResolverAware;
4141
import org.springframework.core.io.Resource;
4242
import org.springframework.core.io.UrlResource;
43+
import org.springframework.core.log.LogFormatUtils;
4344
import org.springframework.http.HttpHeaders;
4445
import org.springframework.http.HttpMethod;
4546
import org.springframework.http.HttpRange;
@@ -662,22 +663,25 @@ private boolean isInvalidEncodedPath(String path) {
662663
protected boolean isInvalidPath(String path) {
663664
if (path.contains("WEB-INF") || path.contains("META-INF")) {
664665
if (logger.isWarnEnabled()) {
665-
logger.warn("Path with \"WEB-INF\" or \"META-INF\": [" + path + "]");
666+
logger.warn(LogFormatUtils.formatValue(
667+
"Path with \"WEB-INF\" or \"META-INF\": [" + path + "]", -1, true));
666668
}
667669
return true;
668670
}
669671
if (path.contains(":/")) {
670672
String relativePath = (path.charAt(0) == '/' ? path.substring(1) : path);
671673
if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) {
672674
if (logger.isWarnEnabled()) {
673-
logger.warn("Path represents URL or has \"url:\" prefix: [" + path + "]");
675+
logger.warn(LogFormatUtils.formatValue(
676+
"Path represents URL or has \"url:\" prefix: [" + path + "]", -1, true));
674677
}
675678
return true;
676679
}
677680
}
678681
if (path.contains("..") && StringUtils.cleanPath(path).contains("../")) {
679682
if (logger.isWarnEnabled()) {
680-
logger.warn("Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]");
683+
logger.warn(LogFormatUtils.formatValue(
684+
"Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]", -1, true));
681685
}
682686
return true;
683687
}

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;
@@ -293,15 +294,17 @@ public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse r
293294

294295
protected void handleInvalidUpgradeHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
295296
if (logger.isErrorEnabled()) {
296-
logger.error("Handshake failed due to invalid Upgrade header: " + request.getHeaders().getUpgrade());
297+
logger.error(LogFormatUtils.formatValue(
298+
"Handshake failed due to invalid Upgrade header: " + request.getHeaders().getUpgrade(), -1, true));
297299
}
298300
response.setStatusCode(HttpStatus.BAD_REQUEST);
299301
response.getBody().write("Can \"Upgrade\" only to \"WebSocket\".".getBytes(StandardCharsets.UTF_8));
300302
}
301303

302304
protected void handleInvalidConnectHeader(ServerHttpRequest request, ServerHttpResponse response) throws IOException {
303305
if (logger.isErrorEnabled()) {
304-
logger.error("Handshake failed due to invalid Connection header " + request.getHeaders().getConnection());
306+
logger.error(LogFormatUtils.formatValue(
307+
"Handshake failed due to invalid Connection header" + request.getHeaders().getConnection(), -1, true));
305308
}
306309
response.setStatusCode(HttpStatus.BAD_REQUEST);
307310
response.getBody().write("\"Connection\" must be \"upgrade\".".getBytes(StandardCharsets.UTF_8));
@@ -325,8 +328,9 @@ protected String[] getSupportedVersions() {
325328
protected void handleWebSocketVersionNotSupported(ServerHttpRequest request, ServerHttpResponse response) {
326329
if (logger.isErrorEnabled()) {
327330
String version = request.getHeaders().getFirst("Sec-WebSocket-Version");
328-
logger.error("Handshake failed due to unsupported WebSocket version: " + version +
329-
". Supported versions: " + Arrays.toString(getSupportedVersions()));
331+
logger.error(LogFormatUtils.formatValue(
332+
"Handshake failed due to unsupported WebSocket version: " + version +
333+
". Supported versions: " + Arrays.toString(getSupportedVersions()), -1, true));
330334
}
331335
response.setStatusCode(HttpStatus.UPGRADE_REQUIRED);
332336
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
@@ -34,6 +34,7 @@
3434
import org.apache.commons.logging.Log;
3535
import org.apache.commons.logging.LogFactory;
3636

37+
import org.springframework.core.log.LogFormatUtils;
3738
import org.springframework.http.HttpHeaders;
3839
import org.springframework.http.HttpMethod;
3940
import org.springframework.http.HttpStatus;
@@ -340,7 +341,8 @@ public final void handleRequest(ServerHttpRequest request, ServerHttpResponse re
340341

341342
if (sockJsPath == null) {
342343
if (logger.isWarnEnabled()) {
343-
logger.warn("Expected SockJS path. Failing request: " + request.getURI());
344+
logger.warn(LogFormatUtils.formatValue(
345+
"Expected SockJS path. Failing request: " + request.getURI(), -1, true));
344346
}
345347
response.setStatusCode(HttpStatus.NOT_FOUND);
346348
return;
@@ -405,7 +407,8 @@ else if (requestInfo != null) {
405407
String[] pathSegments = StringUtils.tokenizeToStringArray(sockJsPath.substring(1), "/");
406408
if (pathSegments.length != 3) {
407409
if (logger.isWarnEnabled()) {
408-
logger.warn("Invalid SockJS path '" + sockJsPath + "' - required to have 3 path segments");
410+
logger.warn(LogFormatUtils.formatValue("Invalid SockJS path '" + sockJsPath + "' - " +
411+
"required to have 3 path segments", -1, true));
409412
}
410413
if (requestInfo != null) {
411414
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)