浏览代码

多重日志记录

gaodm 3 年之前
父节点
当前提交
6fa03e4d42

+ 22 - 1
src/main/java/com/diagbot/facade/LogTestFacade.java

@@ -2,6 +2,7 @@ package com.diagbot.facade;
 
 import com.diagbot.exception.CommonErrorCode;
 import com.diagbot.exception.CommonException;
+import com.diagbot.util.CdssLogUtil;
 import com.diagbot.vo.LogTestVO;
 import org.springframework.stereotype.Component;
 
@@ -17,10 +18,30 @@ public class LogTestFacade {
             //抛错
             throw new CommonException(CommonErrorCode.SERVER_IS_ERROR);
         } else if (logTestVO.getType().equals(2)) {
-
+            paramLog(logTestVO);
         } else {
             /** DO NOTHING*/
         }
         return true;
     }
+
+    /**
+     * 日志里面加入参数
+     *
+     * @param logTestVO
+     * @return
+     */
+    private LogTestVO paramLog(LogTestVO logTestVO) {
+        LogTestVO logTestVOResp = new LogTestVO();
+        logTestVOResp.setMsg("返回参数");
+        logTestVOResp.setType(4);
+        //示例入参设置
+        CdssLogUtil.addBizReq(logTestVO);
+        CdssLogUtil.addBizReq(logTestVO);
+        //出参设置
+        CdssLogUtil.addBizResp(logTestVOResp);
+        CdssLogUtil.addBizResp(logTestVOResp);
+        CdssLogUtil.addBizResp(logTestVOResp);
+        return logTestVOResp;
+    }
 }

+ 78 - 0
src/main/java/com/diagbot/util/CdssLogUtil.java

@@ -10,7 +10,10 @@ import org.springframework.validation.BindException;
 import org.springframework.validation.FieldError;
 import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.MissingServletRequestParameterException;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
 
+import javax.servlet.http.HttpServletResponse;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -21,6 +24,10 @@ import java.util.Map;
  */
 public class CdssLogUtil {
 
+    private final static String CDSS_REQ = "cdss_req";
+    private final static String CDSS_RESP = "cdss_resp";
+    private final static String CDSS_LOG_SP = "→";
+
     /**
      * 入参设置
      *
@@ -66,6 +73,8 @@ public class CdssLogUtil {
         long execTime = System.currentTimeMillis() - start;
         tranLog.setGmtResp(DateUtil.now());
         tranLog.setExecTime(String.valueOf(execTime));
+        //业务处理
+        bizDeal(tranLog);
         return tranLog;
     }
 
@@ -96,6 +105,8 @@ public class CdssLogUtil {
         long execTime = System.currentTimeMillis() - start;
         tranLog.setGmtResp(DateUtil.now());
         tranLog.setExecTime(String.valueOf(execTime));
+        //业务处理
+        bizDeal(tranLog);
         return tranLog;
     }
 
@@ -148,4 +159,71 @@ public class CdssLogUtil {
         resp.msg = e.getMessage();
         return resp;
     }
+
+    /**
+     * 日志处理
+     *
+     * @param tranLog
+     */
+    private static void bizDeal(TranLog tranLog) {
+        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
+        //入参处理
+        String params = tranLog.getParams();
+        String req = response.getHeader(CDSS_REQ);
+        if (StringUtil.isNotBlank(req)) {
+            if (StringUtil.isNotBlank(params)) {
+                params += CDSS_LOG_SP + req;
+            } else {
+                params = req;
+            }
+            response.setHeader(CDSS_REQ, "");
+        }
+        tranLog.setParams(params);
+        //出参处理
+        String result = tranLog.getResult();
+        String resp = response.getHeader(CDSS_RESP);
+        if (StringUtil.isNotBlank(resp)) {
+            if (StringUtil.isNotBlank(result)) {
+                result += CDSS_LOG_SP + resp;
+            } else {
+                result = resp;
+            }
+            response.setHeader(CDSS_RESP, "");
+        }
+        tranLog.setResult(result);
+        //todo 个性化处理
+
+    }
+
+
+    /**
+     * 增加入参
+     *
+     * @param o
+     */
+    public static void addBizReq(Object o) {
+        handleRespHeaderMap(CDSS_REQ, o);
+    }
+
+    /**
+     * 增加出参
+     *
+     * @param o
+     */
+    public static void addBizResp(Object o) {
+        handleRespHeaderMap(CDSS_RESP, o);
+    }
+
+
+    private static void handleRespHeaderMap(String key, Object o) {
+        HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
+        String value = response.getHeader(key);
+        if (StringUtil.isNotBlank(value)) {
+            value += CDSS_LOG_SP + FastJsonUtils.getBeanToJson(o);
+        } else {
+            value = FastJsonUtils.getBeanToJson(o);
+        }
+        // 设置Header
+        response.setHeader(key, value);
+    }
 }

+ 1 - 1
src/main/java/com/diagbot/vo/LogTestVO.java

@@ -14,7 +14,7 @@ import javax.validation.constraints.NotNull;
 @Setter
 public class LogTestVO {
     private String msg;
-    //0:正常,1:抛错,3:子方法放入参数
+    //0:正常,1:抛错,2:子方法放入参数
     @NotNull
     private Integer type = 0;
 }

+ 1 - 1
src/main/java/com/diagbot/web/LogTestController.java

@@ -31,7 +31,7 @@ public class LogTestController {
 
     @ApiOperation(value = "异步日志测试[by:gaodm]",
             notes = "msg:信息内容<br>" +
-                    "type:0:正常,1:抛错,3:子方法放入参数")
+                    "type:0:正常,1:抛错,2:子方法放入参数")
     @PostMapping("/logTest")
     @SysLogger("logTest")
     @CdssLog("异步日志测试")