This is an automated email from the ASF dual-hosted git repository. xiaoyu pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push: new fb4cba876b [type:fix] fix LoggingPlugin error log catch . (#5842) fb4cba876b is described below commit fb4cba876b03dc218340518985fdc45c672f7d45 Author: yunlongn <yunlo...@outlook.com> AuthorDate: Mon Dec 9 16:35:45 2024 +0800 [type:fix] fix LoggingPlugin error log catch . (#5842) --- .../logging/common/AbstractLoggingPlugin.java | 7 ++++- .../logging/console/LoggingConsolePlugin.java | 30 ++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/AbstractLoggingPlugin.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/AbstractLoggingPlugin.java index dca4485834..0374f1af73 100644 --- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/AbstractLoggingPlugin.java +++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-common/src/main/java/org/apache/shenyu/plugin/logging/common/AbstractLoggingPlugin.java @@ -119,7 +119,12 @@ public abstract class AbstractLoggingPlugin<L extends ShenyuRequestLog> extends ServerWebExchange webExchange = exchange.mutate().request(loggingServerHttpRequest) .response(loggingServerHttpResponse).build(); loggingServerHttpResponse.setExchange(webExchange); - return chain.execute(webExchange).doOnError(loggingServerHttpResponse::logError); + try { + return chain.execute(webExchange).doOnError(loggingServerHttpResponse::logError); + } catch (Exception e) { + loggingServerHttpResponse.logError(e); + throw e; + } } /** diff --git a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java index b315f2b805..9352d3f7c7 100644 --- a/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java +++ b/shenyu-plugin/shenyu-plugin-logging/shenyu-plugin-logging-console/src/main/java/org/apache/shenyu/plugin/logging/console/LoggingConsolePlugin.java @@ -38,12 +38,15 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.HttpStatusCode; import org.springframework.http.MediaType; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.http.server.reactive.ServerHttpRequestDecorator; import org.springframework.http.server.reactive.ServerHttpResponse; import org.springframework.http.server.reactive.ServerHttpResponseDecorator; import org.springframework.util.MultiValueMap; +import org.springframework.web.server.ResponseStatusException; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; @@ -97,8 +100,15 @@ public class LoggingConsolePlugin extends AbstractShenyuPlugin { .append(getRequestMethod(request, desensitized, keyWordMatch)).append(System.lineSeparator()) .append(getRequestHeaders(request, desensitized, keyWordMatch)).append(System.lineSeparator()) .append(getQueryParams(request, desensitized, keyWordMatch)).append(System.lineSeparator()); - return chain.execute(exchange.mutate().request(new LoggingServerHttpRequest(request, requestInfo, desensitized, keyWordMatch)) - .response(new LoggingServerHttpResponse(exchange.getResponse(), requestInfo, desensitized, keyWordMatch)).build()); + final LoggingServerHttpResponse loggingServerHttpResponse = new LoggingServerHttpResponse(exchange.getResponse(), requestInfo, desensitized, keyWordMatch); + try { + return chain.execute(exchange.mutate().request(new LoggingServerHttpRequest(request, requestInfo, desensitized, keyWordMatch)) + .response(loggingServerHttpResponse).build()) + .doOnError(loggingServerHttpResponse::logError); + } catch (Exception e) { + loggingServerHttpResponse.logError(e); + throw e; + } } @Override @@ -270,6 +280,22 @@ public class LoggingConsolePlugin extends AbstractShenyuPlugin { }); } + /** + * access error. + * + * @param throwable Exception occurred。 + */ + public void logError(final Throwable throwable) { + HttpStatusCode httpStatus = HttpStatus.INTERNAL_SERVER_ERROR; + if (throwable instanceof ResponseStatusException) { + httpStatus = ((ResponseStatusException) throwable).getStatusCode(); + } + logInfo.append("Response Code: ").append(httpStatus).append(System.lineSeparator()); + logInfo.append(getResponseHeaders()).append(System.lineSeparator()); + logInfo.append("ERROR: ").append(System.lineSeparator()); + logInfo.append(throwable.getMessage()).append(System.lineSeparator()); + } + private String getResponseHeaders() { return System.lineSeparator() + "[Response Headers Start]" + System.lineSeparator() + LoggingConsolePlugin.this.getHeaders(serverHttpResponse.getHeaders(), desensitized, keyWordMatch)