关于springboot:SpringBoot利用AOP输出请求相应日志支持输出RequestBody中的参数

51次阅读

共计 2360 个字符,预计需要花费 6 分钟才能阅读完成。

增加依赖

视状况增加 spring-boot-starter-aop 依赖。

配置 AOP 切面

罕用注解作用:
@Aspect:申明该类为一个注解类;
@Pointcut:定义一个切点,前面追随一个表达式,表达式能够定义为某个 package 下的办法,也能够是自定义注解等;
切点定义好后,就是围绕这个切点做文章了:
@Before: 在切点之前,织入相干代码;
@After: 在切点之后,织入相干代码;
@AfterReturning: 在切点返回内容后,织入相干代码,个别用于对返回值做些加工解决的场景;
@AfterThrowing: 用来解决当织入的代码抛出异样后的逻辑解决;
@Around: 在切入点前后织入代码,并且能够自在的管制何时执行切点;

package com.guomz.demo.aspect;

import com.guomz.demo.util.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 输入每个申请响应的日志
 */
@Component
@Aspect
@Slf4j
public class WebLogAspect {@Pointcut("execution(public * com.guomz.demo.controller..*.*(..))")
    public void webLog(){}

    /**
     * 在切点之前织入
     * @param joinPoint
     * @throws Throwable
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 开始打印申请日志
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 打印申请相干参数
        log.info("========================================== Start ==========================================");
        // 打印申请 url
        log.info("URL            : {}", request.getRequestURL().toString());
        // 打印 Http method
        log.info("HTTP Method    : {}", request.getMethod());
        // 打印调用 controller 的全门路以及执行办法
        log.info("Class Method   : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
        // 打印申请的 IP
        log.info("IP             : {}", request.getRemoteAddr());
        // 打印申请入参
        log.info("Request Args   : {}", JSONUtil.toJson(joinPoint.getArgs()));
    }
    /**
     * 在切点之后织入
     * @throws Throwable
     */
    @After("webLog()")
    public void doAfter() throws Throwable {log.info("=========================================== End ===========================================");
        // 每个申请之间空一行
        log.info("");
    }
    /**
     * 盘绕
     * @param proceedingJoinPoint
     * @return
     * @throws Throwable
     */
    @Around("webLog()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {long startTime = System.currentTimeMillis();
        Object result = proceedingJoinPoint.proceed();
        // 打印出参
        log.info("Response Args  : {}", JSONUtil.toJson(result));
        // 执行耗时
        log.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
        return result;
    }
}

在切点注解中对 controller 包下的全副办法进行了植入,JSONUtil 为本人实现的 json 序列化工具。
本文援用自:
Spring Boot AOP 切面对立打印申请与响应日志

正文完
 0