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 注解修飾:說明當前類 是一個切面類。
- 使用 @Component注解修飾:標記 切面類 為組件,這樣 IOC 容器 會為其實例化和管理(< 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,則會返回 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());
記錄響應結果 使用 jackson 將帝鄉(xiāng)轉換為 json 格式。
log.info("RESPONSE : " + new ObjectMapper().writeValueAsString(res));全部:
/**
* 打印請求和響應信息
*/
@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("========================新的請求========================");
// 收到請求,記錄請求內容
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("URL : " + request.getRequestURL().toString());
log.info("HTTP_METHOD : " + request.getMethod());
// 若是 localhost,則會返回 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());
// 記錄響應
log.info("RESPONSE : " + new ObjectMapper().writeValueAsString(res));
return res;
}
}
總結
到此這篇關于SpringBoot AOP統(tǒng)一處理Web請求日志的文章就介紹到這了,更多相關SpringBoot AOP統(tǒng)一處理Web請求日志內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Java微信公眾平臺開發(fā)(15) 微信JSSDK的使用
這篇文章主要為大家詳細介紹了Java微信公眾平臺開發(fā)第十五步,微信JSSDK的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Go Java算法之外觀數(shù)列實現(xiàn)方法示例詳解
這篇文章主要為大家介紹了Go Java算法外觀數(shù)列實現(xiàn)的方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
SpringBoot實現(xiàn)緩存組件配置動態(tài)切換的步驟詳解
現(xiàn)在有多個springboot項目,但是不同的項目中使用的緩存組件是不一樣的,有的項目使用redis,有的項目使用ctgcache,現(xiàn)在需要用同一套代碼通過配置開關,在不同的項目中切換這兩種緩存,本文介紹了SpringBoot實現(xiàn)緩存組件配置動態(tài)切換的步驟,需要的朋友可以參考下2024-07-07
關于SpingMVC的<context:component-scan>包掃描踩坑記錄
這篇文章主要介紹了關于SpingMVC的<context:component-scan>包掃描踩坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03

