Skip to content

Commit 196f3f8

Browse files
committed
Improve WebFlux exception logging
This commit updates HttpWebHandlerAdapter and ResponseStatusExceptionHandler in order to specify the method/uri in the logged message. It also logs a WARN message for bad request (400) HTTP responses in order to get some logs when an exception is thrown due to client error (unable to deserialize request body for example). Issue: SPR-16447
1 parent fdde9de commit 196f3f8

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

spring-web/src/main/java/org/springframework/web/server/adapter/HttpWebHandlerAdapter.java

Lines changed: 5 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-2018 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.
@@ -182,7 +182,7 @@ public ApplicationContext getApplicationContext() {
182182
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) {
183183
ServerWebExchange exchange = createExchange(request, response);
184184
return getDelegate().handle(exchange)
185-
.onErrorResume(ex -> handleFailure(response, ex))
185+
.onErrorResume(ex -> handleFailure(request, response, ex))
186186
.then(Mono.defer(response::setComplete));
187187
}
188188

@@ -191,7 +191,7 @@ protected ServerWebExchange createExchange(ServerHttpRequest request, ServerHttp
191191
getCodecConfigurer(), getLocaleContextResolver(), this.applicationContext);
192192
}
193193

194-
private Mono<Void> handleFailure(ServerHttpResponse response, Throwable ex) {
194+
private Mono<Void> handleFailure(ServerHttpRequest request, ServerHttpResponse response, Throwable ex) {
195195
if (isDisconnectedClientError(ex)) {
196196
if (disconnectedClientLogger.isTraceEnabled()) {
197197
disconnectedClientLogger.trace("Looks like the client has gone away", ex);
@@ -204,7 +204,8 @@ else if (disconnectedClientLogger.isDebugEnabled()) {
204204
return Mono.empty();
205205
}
206206
if (response.setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR)) {
207-
logger.error("Failed to handle request", ex);
207+
logger.error("Failed to handle request [" + request.getMethod() + " "
208+
+ request.getURI() + "]", ex);
208209
return Mono.empty();
209210
}
210211
// After the response is committed, propagate errors to the server..

spring-web/src/main/java/org/springframework/web/server/handler/ResponseStatusExceptionHandler.java

Lines changed: 16 additions & 2 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-2018 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.
@@ -21,6 +21,7 @@
2121
import reactor.core.publisher.Mono;
2222

2323
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.server.reactive.ServerHttpRequest;
2425
import org.springframework.web.server.ResponseStatusException;
2526
import org.springframework.web.server.ServerWebExchange;
2627
import org.springframework.web.server.WebExceptionHandler;
@@ -42,11 +43,24 @@ public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
4243
if (ex instanceof ResponseStatusException) {
4344
HttpStatus status = ((ResponseStatusException) ex).getStatus();
4445
if (exchange.getResponse().setStatusCode(status)) {
45-
logger.trace(ex.getMessage());
46+
if (status.is5xxServerError()) {
47+
logger.error(buildMessage(exchange.getRequest(), ex));
48+
}
49+
else if (status == HttpStatus.BAD_REQUEST) {
50+
logger.warn(buildMessage(exchange.getRequest(), ex));
51+
}
52+
else {
53+
logger.trace(buildMessage(exchange.getRequest(), ex));
54+
}
4655
return exchange.getResponse().setComplete();
4756
}
4857
}
4958
return Mono.error(ex);
5059
}
5160

61+
private String buildMessage(ServerHttpRequest request, Throwable ex) {
62+
return "Failed to handle request [" + request.getMethod() + " "
63+
+ request.getURI() + "]: " + ex.getMessage();
64+
}
65+
5266
}

0 commit comments

Comments
 (0)