|
@@ -1,14 +1,18 @@
|
|
package com.fdkankan.gateway.filter;
|
|
package com.fdkankan.gateway.filter;
|
|
|
|
|
|
|
|
+import cn.hutool.core.exceptions.ExceptionUtil;
|
|
import cn.hutool.core.net.URLDecoder;
|
|
import cn.hutool.core.net.URLDecoder;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import cn.hutool.core.util.StrUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.fdkankan.common.constant.LoginType;
|
|
import com.fdkankan.common.constant.LoginType;
|
|
|
|
+import com.fdkankan.common.constant.ServerCode;
|
|
import com.fdkankan.common.response.ResultData;
|
|
import com.fdkankan.common.response.ResultData;
|
|
import com.fdkankan.gateway.log.GatewayLog;
|
|
import com.fdkankan.gateway.log.GatewayLog;
|
|
import com.fdkankan.gateway.util.WebUtil;
|
|
import com.fdkankan.gateway.util.WebUtil;
|
|
import com.yomahub.tlog.context.TLogContext;
|
|
import com.yomahub.tlog.context.TLogContext;
|
|
|
|
+import java.io.IOException;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
import org.reactivestreams.Publisher;
|
|
import org.reactivestreams.Publisher;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
|
|
@@ -99,11 +103,13 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
if(MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)
|
|
if(MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)
|
|
|| MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)){
|
|
|| MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)){
|
|
return writeBodyLog(exchange, chain, gatewayLog);
|
|
return writeBodyLog(exchange, chain, gatewayLog);
|
|
- }else{
|
|
|
|
|
|
+ }
|
|
|
|
+ else{
|
|
return writeBasicLog(exchange, chain, gatewayLog);
|
|
return writeBasicLog(exchange, chain, gatewayLog);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+
|
|
private Mono<Void> writeBasicLog(ServerWebExchange exchange, GatewayFilterChain chain, GatewayLog accessLog) {
|
|
private Mono<Void> writeBasicLog(ServerWebExchange exchange, GatewayFilterChain chain, GatewayLog accessLog) {
|
|
StringBuilder builder = new StringBuilder();
|
|
StringBuilder builder = new StringBuilder();
|
|
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
|
MultiValueMap<String, String> queryParams = exchange.getRequest().getQueryParams();
|
|
@@ -116,13 +122,16 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, accessLog);
|
|
ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, accessLog);
|
|
|
|
|
|
return chain.filter(exchange.mutate().response(decoratedResponse).build())
|
|
return chain.filter(exchange.mutate().response(decoratedResponse).build())
|
|
|
|
+ .doOnError(e->{
|
|
|
|
+ // 打印日志
|
|
|
|
+ writeBadAccessLog(accessLog, e);
|
|
|
|
+ })
|
|
.then(Mono.fromRunnable(() -> {
|
|
.then(Mono.fromRunnable(() -> {
|
|
// 打印日志
|
|
// 打印日志
|
|
writeAccessLog(accessLog);
|
|
writeAccessLog(accessLog);
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 参考: org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory
|
|
* 参考: org.springframework.cloud.gateway.filter.factory.rewrite.ModifyRequestBodyGatewayFilterFactory
|
|
* @param exchange
|
|
* @param exchange
|
|
@@ -161,13 +170,27 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
|
|
|
|
// 记录普通的
|
|
// 记录普通的
|
|
return chain.filter(exchange.mutate().request(decoratedRequest).response(decoratedResponse).build())
|
|
return chain.filter(exchange.mutate().request(decoratedRequest).response(decoratedResponse).build())
|
|
- .then(Mono.fromRunnable(() -> {
|
|
|
|
- // 打印日志
|
|
|
|
- writeAccessLog(gatewayLog);
|
|
|
|
- }));
|
|
|
|
|
|
+ .doOnError(e->{
|
|
|
|
+ // 打印日志
|
|
|
|
+ writeBadAccessLog(gatewayLog, e);
|
|
|
|
+ })
|
|
|
|
+ .then(Mono.fromRunnable(() -> {
|
|
|
|
+ // 打印日志
|
|
|
|
+ writeAccessLog(gatewayLog);
|
|
|
|
+ }));
|
|
}));
|
|
}));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private void writeBadAccessLog(GatewayLog gatewayLog, Throwable e){
|
|
|
|
+ gatewayLog.setResponseTime(Calendar.getInstance().getTime());
|
|
|
|
+ gatewayLog.setExecuteTime(gatewayLog.getResponseTime().getTime()-gatewayLog.getRequestTime().getTime());
|
|
|
|
+ gatewayLog.setCode(ServerCode.SYSTEM_ERROR.code());
|
|
|
|
+ gatewayLog.setMessage(ServerCode.SYSTEM_ERROR.message());
|
|
|
|
+ gatewayLog.setResponseData(ExceptionUtil.stacktraceToString(e, 3000));
|
|
|
|
+ this.writeAccessLog(gatewayLog);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 打印日志
|
|
* 打印日志
|
|
* @param gatewayLog 网关日志
|
|
* @param gatewayLog 网关日志
|
|
@@ -175,7 +198,7 @@ public class AccessLogFilter implements GlobalFilter, Ordered {
|
|
private void writeAccessLog(GatewayLog gatewayLog) {
|
|
private void writeAccessLog(GatewayLog gatewayLog) {
|
|
log.info(JSON.toJSONString(gatewayLog));
|
|
log.info(JSON.toJSONString(gatewayLog));
|
|
//日志写入mongodb
|
|
//日志写入mongodb
|
|
- mongoTemplate.insert(gatewayLog, "gatewayLog");
|
|
|
|
|
|
+// mongoTemplate.insert(gatewayLog, "gatewayLog");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|