AOP实现日志记录

需求分析

需要通过日志记录接口调用信息。便于后期调试排查。并且可能有很多接口都需要进行日志的记录。

接口被调用时日志打印格式如下:

        log.info("=======Start=======");
        // 打印请求 URL
        log.info("URL            : {}",);
        // 打印描述信息
        log.info("BusinessName   : {}", );
        // 打印 Http method
        log.info("HTTP Method    : {}", );
        // 打印调用 controller 的全路径以及执行方法
        log.info("Class Method   : {}.{}", );
        // 打印请求的 IP
        log.info("IP             : {}",);
        // 打印请求入参
        log.info("Request Args   : {}",);
        // 打印出参
        log.info("Response       : {}", );
        // 结束后换行
        log.info("=======End=======" + System.lineSeparator());

思路分析

相当于是对原有的功能进行增强。并且是批量的增强,这个时候就非常适合用AOP来进行实现,例如对更新用户的接口进行日志记录,当用户调用这个接口的时候,对调用的信息进行记录

    @PutMapping("/userInfo")
    public ResponseResult updateUserInfo(@RequestBody User user){
        return userService.updateUserInfo(user);
    }

代码实现

定义自定义注解@SystemLog

创建接口

annotation包下创建SystemLog

@Retention(RetentionPolicy.RUNTIME)表示在运行是生效

@Target(ElementType.METHOD)作用类型是方法

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface SystemLog {
    String businessName();
}

定义切面类LogAspect

@Pointcut注解,它主要定义切入点,用来标记一个方法以及在何处触发断点。它可以定义切入点表达式,标记一个方法,以及指定何时执行某个动作。

@Around注解可以用来在调用一个具体方法前和调用后来完成一些具体的任务。比如在在调用pt()方法前调用printLog()方法。

@Aspect //该注解声明这个类为一个切面类
@Component
@Slf4j
public class LogAspect {

    @Pointcut("@annotation(com.whj.annotation.SystemLog)")
    public void pt() {
    }

    @Around("pt()")
    public Object printLog(ProceedingJoinPoint joinPoint) throws Throwable{

        Object ret;
        try {
            handleBefore(joinPoint);
            // 返回执行的结果
            ret = joinPoint.proceed();
            handleAfter(ret);
        } finally {
            // 结束后换行
            log.info("=======End=======" + System.lineSeparator());
        }
        return ret;
    }

    private void handleAfter(Object ret) {
        // 打印出参
        log.info("Response       : {}", JSON.toJSON(ret));
    }

    private void handleBefore(ProceedingJoinPoint joinPoint) {

        // 向下转行  获取被增强方法的URL
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();

        //获取被增强方法的注解对象
        SystemLog systemLog = getSystemLog(joinPoint);

        log.info("=======Start=======");
        // 打印请求 URL
        log.info("URL            : {}",request.getRequestURL());
        // 打印描述信息
        log.info("BusinessName   : {}", systemLog.businessName());
        // 打印 Http method
        log.info("HTTP Method    : {}", request.getMethod());
        // 打印调用 controller 的全路径以及执行方法
        log.info("Class Method   : {}.{}", joinPoint.getSignature().getDeclaringTypeName(),joinPoint.getSignature().getName());
        // 打印请求的 IP
        log.info("IP             : {}",request.getRemoteHost());
        // 打印请求入参
        log.info("Request Args   : {}", JSON.toJSON(joinPoint.getArgs()));
    }

    private SystemLog getSystemLog(ProceedingJoinPoint joinPoint) {

        //将增强方法的整体作为一个对象封装成methodSignature
        MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
        // 获取方法上的注解
        SystemLog systemLog = methodSignature.getMethod().getAnnotation(SystemLog.class);
        return systemLog;
    }
}

在需要记录日志的方法加上注解

    @PutMapping("/userInfo")
    @SystemLog(businessName="更新用户信息")
    public ResponseResult updateUserInfo(@RequestBody User user){
        return userService.updateUserInfo(user);
    }

运行结果

猜你喜欢

转载自juejin.im/post/7237520441554993207