SpringBoot AOP處理請求日志打印功能代碼實例
更新時間:2020年03月26日 09:50:20 作者:趙小胖0914
這篇文章主要介紹了SpringBoot AOP處理請求日志打印功能代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
設計原則和思路:
- 元注解方式結合AOP,靈活記錄操作日志
- 能夠記錄詳細錯誤日志為運營以及審計提供支持
- 日志記錄盡可能減少性能影響
- 操作描述參數支持動態(tài)獲取,其他參數自動記錄。
代碼實例如下
@Slf4j
@Aspect
@Configuration
public class RequestAopConfig {
@Autowired
private HttpServletRequest request;
private static final ThreadLocal<Long> START_TIME_MILLIS = new ThreadLocal<>();
@Pointcut("execution(* com.xxx.xxx.xxx..*(..)) " +
"&&(@annotation(org.springframework.web.bind.annotation.PostMapping)" +
"||@annotation(org.springframework.web.bind.annotation.GetMapping)" +
"||@annotation(org.springframework.web.bind.annotation.PutMapping)" +
"||@annotation(org.springframework.web.bind.annotation.DeleteMapping))")
public void controllerMethodPointcut() {
}
/**
* 前置通知:在某連接點之前執(zhí)行的通知,但這個通知不能阻止連接點之前的執(zhí)行流程(除非它拋出一個異常)。
*
* @param joinPoint 參數
*/
@Before("controllerMethodPointcut()")
public void before(JoinPoint joinPoint) {
START_TIME_MILLIS.set(System.currentTimeMillis());
}
/**
* 后置通知:在某連接點正常完成后執(zhí)行的通知,通常在一個匹配的方法返回的時候執(zhí)行。
*
* @param joinPoint 參數
*/
@AfterReturning(value = "controllerMethodPointcut()", returning = "result")
public void afterReturning(JoinPoint joinPoint, Object result) {
String logTemplate = "--------------- 執(zhí)行成功 ---------------\n請求開始---Send Request URL: {}, Method: {}, Params: {} \n請求方法---ClassName: {}, [Method]: {}, execution time: {}ms \n請求結束---Send Response Result: {}";
log.info(logTemplate, request.getRequestURL(), request.getMethod(), JSON.toJSONString(joinPoint.getArgs()), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), (System.currentTimeMillis() - START_TIME_MILLIS.get()), JSON.toJSONString(result));
START_TIME_MILLIS.remove();
}
/**
* 異常通知:在方法拋出異常退出時執(zhí)行的通知。
*
* @param joinPoint 參數
*/
@AfterThrowing(value = "controllerMethodPointcut()", throwing = "ex")
public void afterThrowing(JoinPoint joinPoint, Throwable ex) {
String logTemplate = "--------------- 執(zhí)行失敗 ---------------\n異常請求開始---Send Request URL: {}, Method: {}, Params: {} \n異常請求方法---ClassName: {}, [Method]: {}, execution time: {}ms \n異常請求結束---Exception Message: {}";
log.error(logTemplate, request.getRequestURL(), request.getMethod(), JSON.toJSONString(joinPoint.getArgs()), joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(), (System.currentTimeMillis() - START_TIME_MILLIS.get()), ex.getMessage());
START_TIME_MILLIS.remove();
}
/**
* 最終通知。當某連接點退出的時候執(zhí)行的通知(不論是正常返回還是異常退出)。
*
* @param joinPoint
*/
@After("controllerMethodPointcut()")
public void after(JoinPoint joinPoint) {
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
RabbitMQ 3.9.7 鏡像模式集群與Springboot 2.5.5 整合
今天我們來聊聊 RabbitMQ 3.9.7 鏡像模式集群與Springboot 2.5.5 整合,本文通過實例代碼給大家介紹的非常詳細,需要的朋友參考下吧2021-10-10
Spring mvc整合tiles框架的簡單入門教程(maven)
這篇文章主要給大家介紹了關于Spring mvc整合tiles框架的簡單入門教程(maven),文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友可以參考借鑒,下面來一起看看詳細的介紹吧。2017-12-12
Spark SerializedLambda錯誤的兩種解決方案
這篇文章主要介紹了Spark SerializedLambda錯誤的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Spring?Boot整合Log4j2.xml的問題及解決方法
這篇文章主要介紹了Spring?Boot整合Log4j2.xml的問題,本文給大家分享解決方案,需要的朋友可以參考下2023-09-09

