天天看點

SpringBoot AOP 記錄WEB請求日志

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/catoop/article/details/71556374

接上一篇文章:

http://blog.csdn.net/catoop/article/details/71541612

實作AOP的切面主要有以下幾個要素:

使用@Aspect注解将一個java類定義為切面類

使用@Pointcut定義一個切入點,可以是一個規則表達式,比如下例中某個package下的所有函數,也可以是一個注解等。

根據需要在切入點不同位置的切入内容

使用@Before在切入點開始處切入内容

使用@After在切入點結尾處切入内容

使用@AfterReturning在切入點return内容之後切入内容(可以用來對處理傳回值做一些加工處理)

使用@Around在切入點前後切入内容,并自己控制何時執行切入點自身的内容

使用@AfterThrowing用來處理當切入内容部分抛出異常之後的處理邏輯

下面附上代碼,我覺得真沒有必要啰嗦的去解釋了,看一下就明了

package com.shanhy.sboot.aoplog;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Aspect
@Component
public class WebLogAspect {

    private static final Logger LOG = LoggerFactory.getLogger(WebLogAspect.class);

    @Pointcut("execution(public * com.shanhy.sboot..controller.*.*(..))")//兩個..代表所有子目錄,最後括号裡的兩個..代表所有參數
    public void logPointCut() {
    }

    @Before("logPointCut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到請求,記錄請求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        // 記錄下請求内容
        LOG.info("請求位址 : " + request.getRequestURL().toString());
        LOG.info("HTTP METHOD : " + request.getMethod());
        LOG.info("IP : " + request.getRemoteAddr());
        LOG.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "."
                + joinPoint.getSignature().getName());
        LOG.info("參數 : " + Arrays.toString(joinPoint.getArgs()));

    }

    @AfterReturning(returning = "ret", pointcut = "logPointCut()")// returning的值和doAfterReturning的參數名一緻
    public void doAfterReturning(Object ret) throws Throwable {
        // 處理完請求,傳回内容
        LOG.info("傳回值 : " + ret);
    }

    @Around("logPointCut()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object ob = pjp.proceed();// ob 為方法的傳回值
        LOG.info("耗時 : " + (System.currentTimeMillis() - startTime));
        return ob;
    }

}
           
package com.shanhy.sboot.demo.controller;

import java.util.concurrent.TimeUnit;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/log")
public class WebLogTestController {

    @RequestMapping("/test")
    public String test() {
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "TEST";
    }

}
           

結束。