SpringBoot AOP處理請(qǐng)求日志打印功能代碼實(shí)例
設(shè)計(jì)原則和思路:
元注解方式結(jié)合AOP,靈活記錄操作日志 能夠記錄詳細(xì)錯(cuò)誤日志為運(yùn)營(yíng)以及審計(jì)提供支持 日志記錄盡可能減少性能影響 操作描述參數(shù)支持動(dòng)態(tài)獲取,其他參數(shù)自動(dòng)記錄。代碼實(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() { } /** * 前置通知:在某連接點(diǎn)之前執(zhí)行的通知,但這個(gè)通知不能阻止連接點(diǎn)之前的執(zhí)行流程(除非它拋出一個(gè)異常)。 * * @param joinPoint 參數(shù) */ @Before('controllerMethodPointcut()') public void before(JoinPoint joinPoint) { START_TIME_MILLIS.set(System.currentTimeMillis()); } /** * 后置通知:在某連接點(diǎn)正常完成后執(zhí)行的通知,通常在一個(gè)匹配的方法返回的時(shí)候執(zhí)行。 * * @param joinPoint 參數(shù) */ @AfterReturning(value = 'controllerMethodPointcut()', returning = 'result') public void afterReturning(JoinPoint joinPoint, Object result) { String logTemplate = '--------------- 執(zhí)行成功 ---------------n請(qǐng)求開(kāi)始---Send Request URL: {}, Method: {}, Params: {} n請(qǐng)求方法---ClassName: {}, [Method]: {}, execution time: {}ms n請(qǐng)求結(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(); } /** * 異常通知:在方法拋出異常退出時(shí)執(zhí)行的通知。 * * @param joinPoint 參數(shù) */ @AfterThrowing(value = 'controllerMethodPointcut()', throwing = 'ex') public void afterThrowing(JoinPoint joinPoint, Throwable ex) { String logTemplate = '--------------- 執(zhí)行失敗 ---------------n異常請(qǐng)求開(kāi)始---Send Request URL: {}, Method: {}, Params: {} n異常請(qǐng)求方法---ClassName: {}, [Method]: {}, execution time: {}ms n異常請(qǐng)求結(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(); } /** * 最終通知。當(dāng)某連接點(diǎn)退出的時(shí)候執(zhí)行的通知(不論是正常返回還是異常退出)。 * * @param joinPoint */ @After('controllerMethodPointcut()') public void after(JoinPoint joinPoint) { }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP實(shí)現(xiàn)加法驗(yàn)證碼2. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向3. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法4. asp(vbs)Rs.Open和Conn.Execute的詳解和區(qū)別及&H0001的說(shuō)明5. CSS hack用法案例詳解6. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息7. asp中response.write("中文")或者js中文亂碼問(wèn)題8. PHP設(shè)計(jì)模式中工廠模式深入詳解9. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲10. 用css截取字符的幾種方法詳解(css排版隱藏溢出文本)
