ToB企服应用市场:ToB评测及商务社交产业平台
标题:
Springcloud Gateway网关日志这样打才完美
[打印本页]
作者:
慢吞云雾缓吐愁
时间:
2022-6-25 09:40
标题:
Springcloud Gateway网关日志这样打才完美
日志打印收集是开发中调试和定位线上问题的关键手段也是重中之重,gateway作为请求入口转发的核心模块,合理、规范的日志打印很重要。
这里只进行日志的打印输出,不做收集,收集汇总工作可以结合ELK,监控日志文件进行同步。
实现方式使用 gateway 的
GlobalFilter
过滤器。
请求日志打印的过滤器排序尽量低一些。
打印日志时,注意避免多次打印造成并发请求日志错乱,可以拼接一个大的日志串,一次打印输出。
RequestLogFilter
增加一个请求入参过滤器,用来打印入参信息。
@Slf4j
@Configuration
@ConditionalOnProperty(value = "log.request.enabled", havingValue = "true", matchIfMissing = true)
public class RequestLogFilter implements GlobalFilter, Ordered {
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
// 打印请求路径
String path = request.getPath().pathWithinApplication().value();
// 打印请求url
String requestUrl = this.getOriginalRequestUrl(exchange);
// **构建成一条长 日志,避免并发下日志错乱**
StringBuilder reqLog = new StringBuilder(200);
// 日志参数
List<Object> reqArgs = new ArrayList<>();
reqLog.append("\n\n================ Gateway Request Start ================\n");
// 打印路由添加占位符
reqLog.append("===> {}: {}\n");
// 参数
String requestMethod = request.getMethodValue();
reqArgs.add(requestMethod);
reqArgs.add(requestUrl);
// 打印请求头
HttpHeaders headers = request.getHeaders();
headers.forEach((headerName, headerValue) -> {
reqLog.append("===Headers=== {}: {}\n");
reqArgs.add(headerName);
//如果有token,可以先把token解析后打印出原始报文,JwtUtil替换成自己项目里工具类
if (AUTH_KEY.toLowerCase().equals(headerName)) {
String value = headerValue.get(0);
String token = JwtUtil.getToken(value);
Claims claims = JwtUtil.parseJWT(token);
reqArgs.add((claims == null) ? "" : claims.toString());
reqLog.append("===Headers=== {}: {}\n");
reqArgs.add(headerName.concat("-original"));
reqArgs.add(StringUtils.join(headerValue.toArray()));
} else {
reqArgs.add(StringUtils.join(headerValue.toArray()));
}
});
reqLog.append("================ Gateway Request End =================\n");
// 打印执行时间
log.info(reqLog.toString(), beforeReqArgs.toArray());
return chain.filter(exchange);
}
private String getOriginalRequestUrl(ServerWebExchange exchange) {
ServerHttpRequest request = exchange.getRequest();
LinkedHashSet<URI> uris = exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR);
URI requestUri = uris.stream().findFirst().orElse(request.getURI());
MultiValueMap<String, String> queryParams = request.getQueryParams();
return UriComponentsBuilder.fromPath(requestUri.getRawPath()).queryParams(queryParams).build().toUriString();
}
@Override
public int getOrder() {
return Ordered.LOWEST_PRECEDENCE;
}
复制代码
ResponseLogFilter
网关请求的响应报文不在这里打印,
post body 参数没有打印
[code]@Slf4j@Configuration@ConditionalOnProperty(value = "log.request.enabled", havingValue = "true", matchIfMissing = true)public class GlobalResponseLogFilter implements GlobalFilter, Ordered { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); // 打印请求路径 String path = request.getPath().pathWithinApplication().value(); return chain.filter(exchange).then( Mono.fromRunnable(() -> { MultiValueMap queryParams = request.getQueryParams(); String requestUrl = UriComponentsBuilder.fromPath(path).queryParams(queryParams).build().toUriString(); // 构建成一条长日志 StringBuilder responseLog = new StringBuilder(200); // 日志参数 List responseArgs = new ArrayList(); responseLog.append("\n\n================ Gateway Response Start ================\n"); ServerHttpResponse response = exchange.getResponse(); // 状态码个path占位符: 200 get: /xxx/xxx/xxx?a=b responseLog.append("
欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/)
Powered by Discuz! X3.4