溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么在SpringBoot中利用AOP處理請求日志

發(fā)布時間:2021-06-12 17:56:28 來源:億速云 閱讀:162 作者:Leah 欄目:編程語言

這期內(nèi)容當中小編將會給大家?guī)碛嘘P怎么在SpringBoot中利用AOP處理請求日志,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

設計原則和思路:

  • 元注解方式結(jié)合AOP,靈活記錄操作日志

  • 能夠記錄詳細錯誤日志為運營以及審計提供支持

  • 日志記錄盡可能減少性能影響

  • 操作描述參數(shù)支持動態(tài)獲取,其他參數(shù)自動記錄。

代碼實例如下

@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 參數(shù)
   */
  @Before("controllerMethodPointcut()")
  public void before(JoinPoint joinPoint) {
    START_TIME_MILLIS.set(System.currentTimeMillis());
  }

  /**
   * 后置通知:在某連接點正常完成后執(zhí)行的通知,通常在一個匹配的方法返回的時候執(zhí)行。
   *
   * @param joinPoint 參數(shù)
   */
  @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請求結(jié)束---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 參數(shù)
   */
  @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異常請求結(jié)束---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) {
  }
}

上述就是小編為大家分享的怎么在SpringBoot中利用AOP處理請求日志了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI