這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在SpringBoot中利用AOP處理請求日志,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
為西峽等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及西峽網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、西峽網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
設(shè)計(jì)原則和思路:
元注解方式結(jié)合AOP,靈活記錄操作日志
能夠記錄詳細(xì)錯誤日志為運(yùn)營以及審計(jì)提供支持
日志記錄盡可能減少性能影響
操作描述參數(shù)支持動態(tài)獲取,其他參數(shù)自動記錄。
代碼實(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í)行的通知,但這個通知不能阻止連接點(diǎn)之前的執(zhí)行流程(除非它拋出一個異常)。 * * @param joinPoint 參數(shù) */ @Before("controllerMethodPointcut()") public void before(JoinPoint joinPoint) { START_TIME_MILLIS.set(System.currentTimeMillis()); } /** * 后置通知:在某連接點(diǎn)正常完成后執(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(); } /** * 最終通知。當(dāng)某連接點(diǎn)退出的時候執(zhí)行的通知(不論是正常返回還是異常退出)。 * * @param joinPoint */ @After("controllerMethodPointcut()") public void after(JoinPoint joinPoint) { } }
上述就是小編為大家分享的怎么在SpringBoot中利用AOP處理請求日志了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
文章標(biāo)題:怎么在SpringBoot中利用AOP處理請求日志
本文網(wǎng)址:http://bm7419.com/article36/gosspg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站改版、企業(yè)網(wǎng)站制作、App設(shè)計(jì)、品牌網(wǎng)站設(shè)計(jì)、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)