天天看點

AOP攔截實作日志統一列印

需求背景:每個接口都有入參出參,如果每次都需要自己寫代碼列印入參出參,大家是否覺得很繁瑣,為了減少備援代碼,提高同時工作效率,減少工作量,于是給代碼加了aop統一攔截。

其實其他項目裡已經有aop攔截,但是需要自己去加注解實作,如果每個接口需要自己手動加一個注解才會列印日志,我依然還是覺得很麻煩,為了大家專注于自己的需求開發,于是進行優化。

首先加注解代碼如下:

/**
 * log注解
 *
 * @author keying
 */
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAnnotation {


}

/**
 * 日志統一列印
 * @author keying
 * @date 2022-11-03 19:40:28
 */
@Aspect
@Component
@Slf4j
public class LogAspect2 {

    private final String REQUEST = "REQUEST URL:{},REQUEST METHOD:{},REQUEST PARAM:{}";
    private final String RESPONSE = "RESPONSE URL:{},RESPONSE METHOD:{},RESPONSE PARAM:{}";

    @Pointcut("@annotation(com.ztocc.tms.operates.controller.config.LogAnnotation)")
    public void requestAspect() {
    }


    @Before(value = "requestAspect()")
    public void methodBefore(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info(REQUEST, request.getRequestURI(), request.getMethod(),
                JSON.toJSONString(Arrays.toString(joinPoint.getArgs())));

    }

    @AfterReturning(returning = "o", pointcut = "requestAspect()")
    public void methodAfter(Object o) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info(RESPONSE, request.getRequestURI(), request.getMethod(),
                JSONObject.toJSONString(o));
    }
}


 @PostMapping("/query_page")
    @LogAnnotation
    public Result<PageVo<ManageScanPageVO>> queryPage(@RequestBody ManageScanPageReq req) {
}      
AOP攔截實作日志統一列印

從圖上可以看到

第一步:寫一個注解,通過Aspect技術把攔截注解修飾的方法。

/**
 * 日志統一列印
 * @author keying
 * @date 2022-11-03 19:40:28
 */
@Aspect
@Component
@Slf4j
public class LogAspect {

    private final String REQUEST = "REQUEST URL:{},REQUEST METHOD:{},REQUEST PARAM:{}";
    private final String RESPONSE = "RESPONSE URL:{},RESPONSE METHOD:{},RESPONSE PARAM:{}";

    /**
     * 攔截controller
     */
    @Pointcut("within(com.ztocc.tms.operates.controller..*)")
    public void requestAspect() {
    }


    @Before(value = "requestAspect()")
    public void methodBefore(JoinPoint joinPoint) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info(REQUEST, request.getRequestURI(), request.getMethod(),
                JSON.toJSONString(Arrays.toString(joinPoint.getArgs())));

    }

    @AfterReturning(returning = "o", pointcut = "requestAspect()")
    public void methodAfter(Object o) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)
                RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        log.info(RESPONSE, request.getRequestURI(), request.getMethod(),
                JSONObject.toJSONString(o));
    }
}