久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot AOP處理請求日志打印功能代碼實例

瀏覽:28日期:2023-05-26 16:54:47

設(shè)計原則和思路:

元注解方式結(jié)合AOP,靈活記錄操作日志 能夠記錄詳細錯誤日志為運營以及審計提供支持 日志記錄盡可能減少性能影響 操作描述參數(shù)支持動態(tài)獲取,其他參數(shù)自動記錄。

代碼實例如下

@Slf4j@Aspect@Configurationpublic 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) { }}

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 保山市| 兖州市| 黄山市| 宜州市| 西乌珠穆沁旗| 兴仁县| 沂水县| 政和县| 呼伦贝尔市| 林州市| 保定市| 东兴市| 遂川县| 海门市| 皋兰县| 泰兴市| 醴陵市| 鄂伦春自治旗| 高淳县| 临沂市| 富蕴县| 泰兴市| 宁都县| 会东县| 弋阳县| 来凤县| 正安县| 巴青县| 秦皇岛市| 南平市| 溧阳市| 石林| 虹口区| 基隆市| 丰都县| 咸丰县| 无锡市| 从江县| 赤壁市| 江城| 广丰县|