Explorar el Código

请求失败也写入日志

dengsixing hace 3 años
padre
commit
93d6a0a48f
Se han modificado 1 ficheros con 27 adiciones y 6 borrados
  1. 27 6
      src/main/java/com/fdkankan/gateway/filter/AccessLogFilter.java

+ 27 - 6
src/main/java/com/fdkankan/gateway/filter/AccessLogFilter.java

@@ -1,9 +1,11 @@
 package com.fdkankan.gateway.filter;
 
+import cn.hutool.core.exceptions.ExceptionUtil;
 import cn.hutool.core.net.URLDecoder;
 import cn.hutool.core.util.StrUtil;
 import com.alibaba.fastjson.JSON;
 import com.fdkankan.common.constant.LoginType;
+import com.fdkankan.common.constant.ServerCode;
 import com.fdkankan.common.response.ResultData;
 import com.fdkankan.gateway.log.GatewayLog;
 import com.fdkankan.gateway.util.WebUtil;
@@ -97,7 +99,8 @@ public class AccessLogFilter  implements GlobalFilter, Ordered {
         MediaType mediaType = request.getHeaders().getContentType();
 
         if(MediaType.APPLICATION_FORM_URLENCODED.isCompatibleWith(mediaType)
-            || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)){
+            || MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)
+            || MediaType.MULTIPART_FORM_DATA.isCompatibleWith(mediaType)){
             return writeBodyLog(exchange, chain, gatewayLog);
         }else{
             return writeBasicLog(exchange, chain, gatewayLog);
@@ -116,6 +119,10 @@ public class AccessLogFilter  implements GlobalFilter, Ordered {
         ServerHttpResponseDecorator decoratedResponse = recordResponseLog(exchange, accessLog);
 
         return chain.filter(exchange.mutate().response(decoratedResponse).build())
+                .doOnError(e->{
+                    // 打印日志
+                    writeBadAccessLog(accessLog, e);
+                })
                 .then(Mono.fromRunnable(() -> {
                     // 打印日志
                     writeAccessLog(accessLog);
@@ -161,13 +168,27 @@ public class AccessLogFilter  implements GlobalFilter, Ordered {
 
                     // 记录普通的
                     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 网关日志
@@ -175,7 +196,7 @@ public class AccessLogFilter  implements GlobalFilter, Ordered {
     private void writeAccessLog(GatewayLog gatewayLog) {
         log.info(JSON.toJSONString(gatewayLog));
         //日志写入mongodb
-//        mongoTemplate.insert(gatewayLog, "gatewayLog");
+        mongoTemplate.insert(gatewayLog, "gatewayLog");
     }