mirror of
https://gitee.com/youlaitech/youlai-mall.git
synced 2025-01-04 01:52:21 +08:00
refactor: 网关请求响应日志拆分
This commit is contained in:
parent
538010e378
commit
4c6b5714bd
@ -37,13 +37,10 @@ import java.util.List;
|
|||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 网关请求响应日志打印
|
* 网关请求响应日志打印
|
||||||
*
|
*
|
||||||
*
|
|
||||||
* @author <a href="mailto:xianrui0365@163.com">haoxr</a>
|
* @author <a href="mailto:xianrui0365@163.com">haoxr</a>
|
||||||
* @see
|
|
||||||
* @date 2022/4/28 17:04
|
* @date 2022/4/28 17:04
|
||||||
*/
|
*/
|
||||||
@ConditionalOnProperty(
|
@ConditionalOnProperty(
|
||||||
@ -77,11 +74,11 @@ public class LogFilter implements GlobalFilter, Ordered {
|
|||||||
) {
|
) {
|
||||||
return writeBodyLog(exchange, chain, traceLog);
|
return writeBodyLog(exchange, chain, traceLog);
|
||||||
} else {
|
} else {
|
||||||
return writeBasicLog(exchange, chain, traceLog);
|
return writeLog(exchange, chain, traceLog);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public Mono<Void> writeBasicLog(ServerWebExchange exchange, GatewayFilterChain chain, TraceLog traceLog) {
|
public Mono<Void> writeLog(ServerWebExchange exchange, GatewayFilterChain chain, TraceLog traceLog) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
||||||
|
|
||||||
@ -92,12 +89,11 @@ public class LogFilter implements GlobalFilter, Ordered {
|
|||||||
if (sb.length() > 0) {
|
if (sb.length() > 0) {
|
||||||
traceLog.setRequestBody(sb.substring(0, sb.length() - 1));
|
traceLog.setRequestBody(sb.substring(0, sb.length() - 1));
|
||||||
}
|
}
|
||||||
|
log.info(traceLog.toRequestString());
|
||||||
ServerHttpResponseDecorator serverHttpResponseDecorator = serverHttpResponseDecorator(exchange, traceLog);
|
ServerHttpResponseDecorator serverHttpResponseDecorator = serverHttpResponseDecorator(exchange, traceLog);
|
||||||
return chain.filter(exchange.mutate().response(serverHttpResponseDecorator)
|
return chain.filter(exchange.mutate().response(serverHttpResponseDecorator)
|
||||||
.build())
|
.build())
|
||||||
.then(Mono.fromRunnable(() -> {
|
.then(Mono.fromRunnable(() -> log.info(traceLog.toResponseString())));
|
||||||
log.info(traceLog.toString());
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -116,6 +112,7 @@ public class LogFilter implements GlobalFilter, Ordered {
|
|||||||
|
|
||||||
Mono<String> cachedBody = serverRequest.bodyToMono(String.class).flatMap(body -> {
|
Mono<String> cachedBody = serverRequest.bodyToMono(String.class).flatMap(body -> {
|
||||||
traceLog.setRequestBody(body);
|
traceLog.setRequestBody(body);
|
||||||
|
log.info(traceLog.toRequestString());
|
||||||
return Mono.just(body);
|
return Mono.just(body);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -130,15 +127,15 @@ public class LogFilter implements GlobalFilter, Ordered {
|
|||||||
ServerHttpRequest serverHttpRequest = serverHttpRequestDecorator(exchange, headers, outputMessage);
|
ServerHttpRequest serverHttpRequest = serverHttpRequestDecorator(exchange, headers, outputMessage);
|
||||||
ServerHttpResponseDecorator serverHttpResponseDecorator = serverHttpResponseDecorator(exchange, traceLog);
|
ServerHttpResponseDecorator serverHttpResponseDecorator = serverHttpResponseDecorator(exchange, traceLog);
|
||||||
return chain.filter(exchange.mutate().request(serverHttpRequest).response(serverHttpResponseDecorator).build())
|
return chain.filter(exchange.mutate().request(serverHttpRequest).response(serverHttpResponseDecorator).build())
|
||||||
.then(Mono.fromRunnable(() -> {
|
.then(Mono.fromRunnable(() -> log.info(traceLog.toResponseString())));
|
||||||
log.info(traceLog.toString());
|
|
||||||
}));
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private ServerHttpRequestDecorator serverHttpRequestDecorator(ServerWebExchange exchange, HttpHeaders headers,
|
private ServerHttpRequestDecorator serverHttpRequestDecorator(ServerWebExchange exchange,
|
||||||
CachedBodyOutputMessage outputMessage) {
|
HttpHeaders headers,
|
||||||
|
CachedBodyOutputMessage outputMessage
|
||||||
|
) {
|
||||||
return new ServerHttpRequestDecorator(exchange.getRequest()) {
|
return new ServerHttpRequestDecorator(exchange.getRequest()) {
|
||||||
@Override
|
@Override
|
||||||
public HttpHeaders getHeaders() {
|
public HttpHeaders getHeaders() {
|
||||||
|
@ -47,6 +47,21 @@ public class TraceLog {
|
|||||||
private Long executeTime;
|
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
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "========网关请求响应日志========\n" +
|
return "========网关请求响应日志========\n" +
|
||||||
|
Loading…
Reference in New Issue
Block a user