springboot 配置aop切面记录请求日志信息

该项目展示如何使用 aop 切面对请求进行日志记录,以及记录 UserAgent 的信息。

一、新建springboot配置pom文件如下:



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.7.RELEASE
         
    
    com.dyz
    aoplog
    0.0.1-SNAPSHOT
    aoplog
    Demo project for Spring Boot

    
        1.8
    

    


        
            org.springframework.boot
            spring-boot-starter
        


        

        
            org.springframework.boot
            spring-boot-starter-aop
        

        
        
        
            eu.bitwalker
            UserAgentUtils
            1.21
        


        
            cn.hutool
            hutool-all
            5.3.3
        


        
            org.projectlombok
            lombok
            true
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        

        
            org.springframework.boot
            spring-boot-starter-web
        

    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


二、新建切面配置类AopLog,如下:

/**
 * @author dyz
 * @version 1.0
 * @date 2020/5/8 16:17
 *
 * 使用 aop 切面记录请求日志信息
 */
@Aspect
@Component
@Slf4j
public class AopLog {
    private static final String START_TIME = "request-start";

    /**
     * 设置切入点
     * 切入controller包下面的所有Controller
     */
    @Pointcut("execution(public * com.dyz.aoplog.controller.*Controller.*(..))")
    public void log() {

    }

    /**
     * 配置前置操作
     * @param point
     */
    @Before("log()")
    public void beforeLog(JoinPoint point) {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        log.info("【请求 URL】:{}", request.getRequestURL());
        log.info("【请求 IP】:{}", request.getRemoteAddr());
        log.info("【请求类名】:{},【请求方法名】:{}", point.getSignature().getDeclaringTypeName(), point.getSignature().getName());

        Map parameterMap = request.getParameterMap();
        log.info("【请求参数】:{},", JSONUtil.toJsonStr(parameterMap));
        Long start = System.currentTimeMillis();
        request.setAttribute(START_TIME, start);
    }

    /**
     * 配置环绕操作
     *
     * @param point 切入点
     * @return 原方法返回值
     * @throws Throwable 异常信息
     */
    @Around("log()")
    public Object aroundLog(ProceedingJoinPoint point) throws Throwable {
        Object result = point.proceed();
        log.info("【返回值】:{}", JSONUtil.toJsonStr(result));
        return result;
    }

    /**
     * 配置后置操作
     */
    @AfterReturning("log()")
    public void afterReturning() {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();

        Long start = (Long) request.getAttribute(START_TIME);
        Long end = System.currentTimeMillis();
        log.info("【请求耗时】:{}毫秒", end - start);

        String header = request.getHeader("User-Agent");
        UserAgent userAgent = UserAgent.parseUserAgentString(header);
        log.info("【浏览器类型】:{},【操作系统】:{},【原始User-Agent】:{}", userAgent.getBrowser().toString(), userAgent.getOperatingSystem().toString(), header);
    }

}

三、新建Controller进行测试

/**
 * @author dyz
 * @version 1.0
 * @date 2020/5/8 16:42
 */
@RestController
public class Controller {

    /**
     * 切面日志测试
     * @param name
     * @return
     */
    @GetMapping("/test")
    public String test(String name) {
        return "访问者:"+name;
    }
}

四、访问接口,并展示打印日志

springboot 配置aop切面记录请求日志信息_第1张图片

springboot 配置aop切面记录请求日志信息_第2张图片

五、源码地址:https://github.com/MichaelDYZ/springboot-aoplog

你可能感兴趣的:(java,springboot,java,springboot,aop,切面编程,日志切面)