SpringBoot AOP處理請(qǐng)求日志打印功能代碼實(shí)例
更新時(shí)間:2020年03月26日 09:50:20 作者:趙小胖0914
這篇文章主要介紹了SpringBoot AOP處理請(qǐng)求日志打印功能代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
設(shè)計(jì)原則和思路:
- 元注解方式結(jié)合AOP,靈活記錄操作日志
- 能夠記錄詳細(xì)錯(cuò)誤日志為運(yùn)營(yíng)以及審計(jì)提供支持
- 日志記錄盡可能減少性能影響
- 操作描述參數(shù)支持動(dòng)態(tài)獲取,其他參數(shù)自動(dòng)記錄。
代碼實(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() { } /** * 前置通知:在某連接點(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í)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- springboot整合mybatis將sql打印到日志的實(shí)例詳解
- springboot配置aop切面日志打印過(guò)程解析
- springboot+mybatis配置控制臺(tái)打印sql日志的方法
- springboot下mybatis-plus如何打印sql日志和參數(shù)到日志文件
- springboot打印接口調(diào)用日志的實(shí)例
- SpringBoot項(xiàng)目實(shí)現(xiàn)日志打印SQL的常用方法(包括SQL語(yǔ)句和參數(shù))
- 在SpringBoot框架中實(shí)現(xiàn)打印響應(yīng)的日志
- SpringBoot整合MyBatis和MyBatis-Plus請(qǐng)求后不打印sql日志的問(wèn)題解決
相關(guān)文章
Java?SimpleDateFormat線程不安全問(wèn)題
這篇文章詳細(xì)介紹了如可解決impleDateFormat線程不安全的問(wèn)題,對(duì)多線程問(wèn)題感興趣的同學(xué)可以參考閱讀本文2023-03-03RabbitMQ 3.9.7 鏡像模式集群與Springboot 2.5.5 整合
今天我們來(lái)聊聊 RabbitMQ 3.9.7 鏡像模式集群與Springboot 2.5.5 整合,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-10-10Spring mvc整合tiles框架的簡(jiǎn)單入門(mén)教程(maven)
這篇文章主要給大家介紹了關(guān)于Spring mvc整合tiles框架的簡(jiǎn)單入門(mén)教程(maven),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考借鑒,下面來(lái)一起看看詳細(xì)的介紹吧。2017-12-12Java求素?cái)?shù)和最大公約數(shù)的簡(jiǎn)單代碼示例
這篇文章主要介紹了Java求素?cái)?shù)和最大公約數(shù)的簡(jiǎn)單代碼示例,其中作者創(chuàng)建的Fraction類(lèi)可以用來(lái)進(jìn)行各種分?jǐn)?shù)運(yùn)算,需要的朋友可以參考下2015-09-09Spark SerializedLambda錯(cuò)誤的兩種解決方案
這篇文章主要介紹了Spark SerializedLambda錯(cuò)誤的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Spring?Boot整合Log4j2.xml的問(wèn)題及解決方法
這篇文章主要介紹了Spring?Boot整合Log4j2.xml的問(wèn)題,本文給大家分享解決方案,需要的朋友可以參考下2023-09-09