From 4c6b5714bd13d9db2d612d933c6e646cd9572993 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=83=9D=E5=85=88=E7=91=9E?= <1490493387@qq.com>
Date: Sun, 1 May 2022 00:39:18 +0800
Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=BD=91=E5=85=B3=E8=AF=B7?=
=?UTF-8?q?=E6=B1=82=E5=93=8D=E5=BA=94=E6=97=A5=E5=BF=97=E6=8B=86=E5=88=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/youlai/gateway/log/LogFilter.java | 23 ++++++++-----------
.../java/com/youlai/gateway/log/TraceLog.java | 15 ++++++++++++
2 files changed, 25 insertions(+), 13 deletions(-)
diff --git a/youlai-gateway/src/main/java/com/youlai/gateway/log/LogFilter.java b/youlai-gateway/src/main/java/com/youlai/gateway/log/LogFilter.java
index ca1d71be0..ea24739fe 100644
--- a/youlai-gateway/src/main/java/com/youlai/gateway/log/LogFilter.java
+++ b/youlai-gateway/src/main/java/com/youlai/gateway/log/LogFilter.java
@@ -37,13 +37,10 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
-
/**
* 网关请求响应日志打印
*
- *
* @author haoxr
- * @see
* @date 2022/4/28 17:04
*/
@ConditionalOnProperty(
@@ -77,11 +74,11 @@ public class LogFilter implements GlobalFilter, Ordered {
) {
return writeBodyLog(exchange, chain, traceLog);
} else {
- return writeBasicLog(exchange, chain, traceLog);
+ return writeLog(exchange, chain, traceLog);
}
}
- public Mono writeBasicLog(ServerWebExchange exchange, GatewayFilterChain chain, TraceLog traceLog) {
+ public Mono writeLog(ServerWebExchange exchange, GatewayFilterChain chain, TraceLog traceLog) {
StringBuilder sb = new StringBuilder();
MultiValueMap queryParams = exchange.getRequest().getQueryParams();
@@ -92,12 +89,11 @@ public class LogFilter implements GlobalFilter, Ordered {
if (sb.length() > 0) {
traceLog.setRequestBody(sb.substring(0, sb.length() - 1));
}
+ log.info(traceLog.toRequestString());
ServerHttpResponseDecorator serverHttpResponseDecorator = serverHttpResponseDecorator(exchange, traceLog);
return chain.filter(exchange.mutate().response(serverHttpResponseDecorator)
.build())
- .then(Mono.fromRunnable(() -> {
- log.info(traceLog.toString());
- }));
+ .then(Mono.fromRunnable(() -> log.info(traceLog.toResponseString())));
}
@@ -116,6 +112,7 @@ public class LogFilter implements GlobalFilter, Ordered {
Mono cachedBody = serverRequest.bodyToMono(String.class).flatMap(body -> {
traceLog.setRequestBody(body);
+ log.info(traceLog.toRequestString());
return Mono.just(body);
});
@@ -130,15 +127,15 @@ public class LogFilter implements GlobalFilter, Ordered {
ServerHttpRequest serverHttpRequest = serverHttpRequestDecorator(exchange, headers, outputMessage);
ServerHttpResponseDecorator serverHttpResponseDecorator = serverHttpResponseDecorator(exchange, traceLog);
return chain.filter(exchange.mutate().request(serverHttpRequest).response(serverHttpResponseDecorator).build())
- .then(Mono.fromRunnable(() -> {
- log.info(traceLog.toString());
- }));
+ .then(Mono.fromRunnable(() -> log.info(traceLog.toResponseString())));
}));
}
- private ServerHttpRequestDecorator serverHttpRequestDecorator(ServerWebExchange exchange, HttpHeaders headers,
- CachedBodyOutputMessage outputMessage) {
+ private ServerHttpRequestDecorator serverHttpRequestDecorator(ServerWebExchange exchange,
+ HttpHeaders headers,
+ CachedBodyOutputMessage outputMessage
+ ) {
return new ServerHttpRequestDecorator(exchange.getRequest()) {
@Override
public HttpHeaders getHeaders() {
diff --git a/youlai-gateway/src/main/java/com/youlai/gateway/log/TraceLog.java b/youlai-gateway/src/main/java/com/youlai/gateway/log/TraceLog.java
index b50cdef7d..f90a7660e 100644
--- a/youlai-gateway/src/main/java/com/youlai/gateway/log/TraceLog.java
+++ b/youlai-gateway/src/main/java/com/youlai/gateway/log/TraceLog.java
@@ -47,6 +47,21 @@ public class TraceLog {
private Long executeTime;
+ public String toRequestString() {
+ return
+ "^^^^^^^^请求日志:" + requestMethod + ':' + requestPath + " ^^^^^^^^\n" +
+ "请求参数:" + requestBody + '\n' +
+ "请求时间:" + requestTime;
+ }
+
+ public String toResponseString() {
+ return
+ "$$$$$$$$响应日志:" + requestMethod + ':' + requestPath + " $$$$$$$$\n" +
+ "响应时间:" + responseTime + '\n' +
+ "响应数据:" + responseBody + '\n' +
+ "执行耗时:" + executeTime + "毫秒";
+ }
+
@Override
public String toString() {
return "========网关请求响应日志========\n" +