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判斷某個(gè)文件是否存在的函數(shù)2. Android table布局開(kāi)發(fā)實(shí)現(xiàn)簡(jiǎn)單計(jì)算器3. 理解PHP5中static和const關(guān)鍵字4. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)5. 在IDEA中實(shí)現(xiàn)同時(shí)運(yùn)行2個(gè)相同的java程序6. Java如何基于反射機(jī)制獲取不同的類(lèi)7. IntelliJ IDEA安裝插件的方法步驟8. Python random庫(kù)使用方法及異常處理方案9. .Net Core使用Coravel實(shí)現(xiàn)任務(wù)調(diào)度的完整步驟10. ASP.NET泛型三之使用協(xié)變和逆變實(shí)現(xiàn)類(lèi)型轉(zhuǎn)換

網(wǎng)公網(wǎng)安備