fix():更新日志模板
parent
23b08aaf35
commit
68e5411860
|
@ -102,7 +102,7 @@ public class AccessLog {
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
|
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
|
||||||
.append("请求简略信息",
|
.append("请求简略信息",
|
||||||
StringUtils.format("[{}:{}:{}]-[{{}-{}}:{} ---> {}]",userId, userIp, traceId, schema, requestMethod, requestUrl,httpStatus)
|
StringUtils.format("[userId:[{}]-userIp:[{}]-traceId:[{}]] ---结果--- {{}-{}}:{} ---> {}",userId, userIp, traceId, schema, requestMethod, requestUrl,httpStatus)
|
||||||
)
|
)
|
||||||
.append("路由", route)
|
.append("路由", route)
|
||||||
.append("查询参数", queryParams)
|
.append("查询参数", queryParams)
|
||||||
|
|
|
@ -1,9 +1,6 @@
|
||||||
package com.muyu.gateway.filter.log;
|
package com.muyu.gateway.filter.log;
|
||||||
|
|
||||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||||
import cn.hutool.core.map.MapUtil;
|
|
||||||
import cn.hutool.json.JSONUtil;
|
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
|
||||||
import com.alibaba.nacos.common.utils.StringUtils;
|
import com.alibaba.nacos.common.utils.StringUtils;
|
||||||
import com.muyu.common.core.constant.SecurityConstants;
|
import com.muyu.common.core.constant.SecurityConstants;
|
||||||
import com.muyu.gateway.utils.WebFrameworkUtils;
|
import com.muyu.gateway.utils.WebFrameworkUtils;
|
||||||
|
@ -40,7 +37,6 @@ import reactor.core.publisher.Mono;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -66,7 +62,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getOrder() {
|
public int getOrder() {
|
||||||
return Ordered.HIGHEST_PRECEDENCE;
|
return -99;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -74,24 +70,25 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
||||||
// 将 Request 中可以直接获取到的参数,设置到网关日志
|
// 将 Request 中可以直接获取到的参数,设置到网关日志
|
||||||
ServerHttpRequest request = exchange.getRequest();
|
ServerHttpRequest request = exchange.getRequest();
|
||||||
// TODO traceId
|
// TODO traceId
|
||||||
AccessLog gatewayLog = new AccessLog();
|
AccessLog accessLog = AccessLog.builder()
|
||||||
gatewayLog.setUserId(request.getHeaders().getFirst(SecurityConstants.DETAILS_USER_ID));
|
.userId(request.getHeaders().getFirst(SecurityConstants.DETAILS_USER_ID))
|
||||||
gatewayLog.setRoute(WebFrameworkUtils.getGatewayRoute(exchange));
|
.route(WebFrameworkUtils.getGatewayRoute(exchange))
|
||||||
gatewayLog.setSchema(request.getURI().getScheme());
|
.schema(request.getURI().getScheme())
|
||||||
gatewayLog.setRequestMethod(request.getMethod().name());
|
.requestMethod(request.getMethod().name())
|
||||||
gatewayLog.setRequestUrl(request.getURI().getRawPath());
|
.requestUrl(request.getURI().getRawPath())
|
||||||
gatewayLog.setQueryParams(request.getQueryParams());
|
.queryParams(request.getQueryParams())
|
||||||
gatewayLog.setRequestHeaders(request.getHeaders());
|
.requestHeaders(request.getHeaders())
|
||||||
gatewayLog.setStartTime(LocalDateTime.now());
|
.startTime(LocalDateTime.now())
|
||||||
gatewayLog.setUserIp(WebFrameworkUtils.getClientIP(exchange));
|
.userIp(WebFrameworkUtils.getClientIP(exchange))
|
||||||
|
.build();
|
||||||
|
|
||||||
// 继续 filter 过滤
|
// 继续 filter 过滤
|
||||||
MediaType mediaType = request.getHeaders().getContentType();
|
MediaType mediaType = request.getHeaders().getContentType();
|
||||||
if (MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)
|
return MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType) || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)
|
||||||
|| MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) { // 适合 JSON 和 Form 提交的请求
|
?
|
||||||
return filterWithRequestBody(exchange, chain, gatewayLog);
|
filterWithRequestBody(exchange, chain, accessLog)
|
||||||
}
|
:
|
||||||
return filterWithoutRequestBody(exchange, chain, gatewayLog);
|
filterWithoutRequestBody(exchange, chain, accessLog);
|
||||||
}
|
}
|
||||||
|
|
||||||
private Mono<Void> filterWithoutRequestBody(ServerWebExchange exchange, GatewayFilterChain chain, AccessLog accessLog) {
|
private Mono<Void> filterWithoutRequestBody(ServerWebExchange exchange, GatewayFilterChain chain, AccessLog accessLog) {
|
||||||
|
|
Loading…
Reference in New Issue