一 目的。
通過使用Spring的aop中point.getArgs()方法可以獲取body參數,這種對源代碼的侵入性比較小,優先選擇。
二 最終的日志展示效果
請求接口時間:2022-01-06 17:08:44,信息為: 【request_id】:b541f6ce42d249e781233b5fbb911efd 【請求 URL】:http://localhost:802/busquery/testloghandle 【請求 IP】:0:0:0:0:0:0:0:1 【請求類名】:com.siisoo.ontimebusquery.controller.QueryController【請求方法名】:testLogHandle 【body】:[{"age":"20","phone":"1234567"}] 【請求參數】:{"is_up_down":["1"],"line_no":["9"]}
三 切面類源碼
1 package com.siisoo.ontimebusquery.handle; 2 3 import cn.hutool.core.util.IdUtil; 4 import com.alibaba.fastjson.JSON; 5 import com.siisoo.ontimebusquery.util.DateUtil; 6 import org.aspectj.lang.ProceedingJoinPoint; 7 import org.aspectj.lang.annotation.Around; 8 import org.aspectj.lang.annotation.Aspect; 9 import org.aspectj.lang.annotation.Pointcut; 10 import org.slf4j.Logger; 11 import org.slf4j.LoggerFactory; 12 import org.springframework.stereotype.Component; 13 import org.springframework.web.bind.annotation.ControllerAdvice; 14 import org.springframework.web.bind.annotation.ResponseBody; 15 import org.springframework.web.bind.annotation.RestControllerAdvice; 16 import org.springframework.web.context.request.RequestAttributes; 17 import org.springframework.web.context.request.RequestContextHolder; 18 import org.springframework.web.context.request.ServletRequestAttributes; 19 20 import javax.servlet.http.HttpServletRequest; 21 import java.util.Map; 22 23 /** 24 25 * REST接口統一的日志處理 26 */ 27 @Aspect 28 @Component 29 public class LogHandle { 30 private final Logger logger = LoggerFactory.getLogger(this.getClass()); 31 @Pointcut("execution(* com.siisoo.ontimebusquery.controller..*.*(..))") 32 public void restLog(){} 33 @Around("restLog()") 34 public void doAround(ProceedingJoinPoint joinPoint) throws Throwable { 35 // 生成本次請求時間戳 36 String timestamp = System.currentTimeMillis()+""; 37 RequestAttributes ra = RequestContextHolder.getRequestAttributes(); 38 ServletRequestAttributes sra = (ServletRequestAttributes) ra; 39 HttpServletRequest request = sra.getRequest(); 40 String url = request.getRequestURL().toString(); 41 String method = request.getMethod(); 42 String uri = request.getRequestURI(); 43 String queryString = request.getQueryString(); 44 Map<String, String[]> parameterMap = request.getParameterMap(); 45 StringBuffer sb = new StringBuffer(); 46 String requestId = IdUtil.simpleUUID(); 47 sb.append("\n【request_id】:").append(requestId); 48 sb.append("\n【請求 URL】:").append(request.getRequestURL()); 49 sb.append("\n【請求 IP】:").append(getIp(request)); 50 sb.append("\n【請求類名】:").append(joinPoint.getSignature().getDeclaringTypeName()); 51 sb.append("【請求方法名】:").append(joinPoint.getSignature().getName()); 52 sb.append("\n【body】:").append(JSON.toJSONString(joinPoint.getArgs())); 53 sb.append("\n【請求參數】:").append(JSON.toJSONString(parameterMap)); 54 55 String requestLog=sb.toString(); 56 57 logger.info(" \n 請求接口時間:"+ DateUtil.getCurrentDateTime() + ",信息為:{} ", requestLog); 58 // result的值就是被攔截方法的返回值 59 Object result = joinPoint.proceed(); 60 logger.info(timestamp + " , " + result.toString()); 61 } 62 63 private String getIp(HttpServletRequest request) { 64 String ip = request.getHeader("x-forwarded-for"); 65 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 66 ip = request.getHeader("Proxy-Client-IP"); 67 } 68 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 69 ip = request.getHeader("WL-Proxy-Client-IP"); 70 } 71 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { 72 ip = request.getRemoteAddr(); 73 } 74 return ip; 75 } 76 77 }
四 pom.xml中的maven依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.18</version>
</dependency>
<dependency>
<groupId>eu.bitwalker</groupId>
<artifactId>UserAgentUtils</artifactId>
<version>1.21</version>
</dependency>
五 請求測試
可以看到,分別記錄下了requestparameter以及requestbody的所傳進來的所有參數信息,方便后續業務中進行BUG定位。