欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot?AOP統(tǒng)一處理Web請(qǐng)求日志的示例代碼

 更新時(shí)間:2023年02月09日 14:47:57   作者:TryMyBestTo  
springboot有很多方法處理日志,例如攔截器,aop切面,service中代碼記錄等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot?AOP統(tǒng)一處理Web請(qǐng)求日志的相關(guān)資料,需要的朋友可以參考下

SpringBoot AOP統(tǒng)一處理Web請(qǐng)求日志

引入依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>
  • 新建過(guò)濾器 WebLogAspect.java
    • 使用 @Aspect 注解修飾:說(shuō)明當(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) {}

記錄請(qǐng)求 URL:

ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
log.info("URL : " + request.getRequestURL().toString());

請(qǐng)求類型:

log.info("HTTP_METHOD : " + request.getMethod());

IP 地址:

  • 若是本地 localhost,則會(huì)返回 ipv6 0:0:0:0:0:0:0:1
log.info("IP : " + request.getRemoteAddr());

記錄請(qǐng)求的 類以及方法:

  • 使用 環(huán)繞通知的 ProceedingJoinPoint 參數(shù)。
log.info("CLASS_METHOD : " + pjp.getSignature().getDeclaringTypeName() + "." + pjp.getSignature().getName());

記錄請(qǐng)求參數(shù):

log.info("ARGS : " + Arrays.toString(pjp.getArgs()));

執(zhí)行方法,處理請(qǐng)求

Object res = pjp.proceed(pjp.getArgs());

記錄響應(yīng)結(jié)果 使用 jackson 將帝鄉(xiāng)轉(zhuǎn)換為 json 格式。

log.info("RESPONSE : " + new ObjectMapper().writeValueAsString(res));

全部:

/**
 * 打印請(qǐng)求和響應(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("========================新的請(qǐng)求========================");
        // 收到請(qǐng)求,記錄請(qǐng)求內(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í)行方法,處理請(qǐng)求
        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請(qǐng)求日志的文章就介紹到這了,更多相關(guān)SpringBoot AOP統(tǒng)一處理Web請(qǐng)求日志內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論