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" +