SpringBoot?AOP統(tǒng)一處理Web請求日志的示例代碼
SpringBoot AOP統(tǒng)一處理Web請求日志
引入依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
- 新建過濾器 WebLogAspect.java
- 使用 @Aspect 注解修飾:說明當(dāng)前類 是一個(gè)切面類。
- 使用 @Component注解修飾:標(biāo)記 切面類 為組件,這樣 IOC 容器 會(huì)為其實(shí)例化和管理(< bean>)
- 定義切面方法:webLog() 方法名隨意
- 使用環(huán)繞通知 @Around(“execution(public * com.daxiong.mall.Controller..(…))”) 注解修飾指定 切面范圍。
@Around("execution(public * com.daxiong.mall.controller.*.*(..))") public Object webLog(ProceedingJoinPoint pjp) {}
記錄請求 URL:
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); log.info("URL : " + request.getRequestURL().toString());
請求類型:
log.info("HTTP_METHOD : " + request.getMethod());
IP 地址:
- 若是本地 localhost,則會(huì)返回 ipv6 0:0:0:0:0:0:0:1
log.info("IP : " + request.getRemoteAddr());
記錄請求的 類以及方法:
- 使用 環(huán)繞通知的 ProceedingJoinPoint 參數(shù)。
log.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());
記錄請求參數(shù):
log.info("ARGS : " + Arrays.toString(pjp.getArgs()));
執(zhí)行方法,處理請求
Object res = pjp.proceed(pjp.getArgs());
記錄響應(yīng)結(jié)果 使用 jackson 將帝鄉(xiāng)轉(zhuǎn)換為 json 格式。
log.info("RESPONSE : " + new ObjectMapper().writeValueAsString(res));
全部:
/** * 打印請求和響應(yīng)信息 */ @Aspect @Component public class WebLogAspect { private final Logger log = LoggerFactory.getLogger(WebLogAspect.class); @Around("execution(public * com.daxiong.mall.controller.*.*(..))") public Object webLog(ProceedingJoinPoint pjp) throws Throwable { log.info("========================新的請求========================"); // 收到請求,記錄請求內(nèi)容 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); log.info("URL : " + request.getRequestURL().toString()); log.info("HTTP_METHOD : " + request.getMethod()); // 若是 localhost,則會(huì)返回 0:0:0:0:0:0:0:1 log.info("IP : " + request.getRemoteAddr()); log.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName()); log.info("ARGS : " + Arrays.toString(pjp.getArgs())); // 執(zhí)行方法,處理請求 Object res = pjp.proceed(pjp.getArgs()); // 記錄響應(yīng) log.info("RESPONSE : " + new ObjectMapper().writeValueAsString(res)); return res; } }
總結(jié)
到此這篇關(guān)于SpringBoot AOP統(tǒng)一處理Web請求日志的文章就介紹到這了,更多相關(guān)SpringBoot AOP統(tǒng)一處理Web請求日志內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java微信公眾平臺(tái)開發(fā)(15) 微信JSSDK的使用
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺(tái)開發(fā)第十五步,微信JSSDK的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Go Java算法之外觀數(shù)列實(shí)現(xiàn)方法示例詳解
這篇文章主要為大家介紹了Go Java算法外觀數(shù)列實(shí)現(xiàn)的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08SpringBoot實(shí)現(xiàn)緩存組件配置動(dòng)態(tài)切換的步驟詳解
現(xiàn)在有多個(gè)springboot項(xiàng)目,但是不同的項(xiàng)目中使用的緩存組件是不一樣的,有的項(xiàng)目使用redis,有的項(xiàng)目使用ctgcache,現(xiàn)在需要用同一套代碼通過配置開關(guān),在不同的項(xiàng)目中切換這兩種緩存,本文介紹了SpringBoot實(shí)現(xiàn)緩存組件配置動(dòng)態(tài)切換的步驟,需要的朋友可以參考下2024-07-07java中ThreadLocal的應(yīng)用場景實(shí)例分析
在本篇文章里小編給大家整理的是一篇關(guān)于java中ThreadLocal的應(yīng)用場景實(shí)例分析,對此有興趣的朋友們可以學(xué)習(xí)參考下。2021-02-02關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄
這篇文章主要介紹了關(guān)于SpingMVC的<context:component-scan>包掃描踩坑記錄,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03Java日常練習(xí)題,每天進(jìn)步一點(diǎn)點(diǎn)(42)
下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,希望可以幫到你2021-07-07