AOP日志

作者: knock | 来源:发表于2020-07-25 16:43 被阅读0次

WebLog.java

package kr.weitao.starter.config.annotation;


import java.lang.annotation.*;


/**
 * @author yyd
 * web日志注解
 */
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface WebLog {

    /**
     * 日志描述信息
     *
     * @return
     */
    String description() default "";
}

WebLogAspect.java

package kr.weitao.starter.config;

import com.google.gson.Gson;
import kr.weitao.starter.config.annotation.WebLog;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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.springframework.context.annotation.Configuration;
import org.springframework.http.ResponseEntity;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.lang.reflect.Method;
import java.util.Objects;

@Aspect
@Configuration
@Slf4j
public class WebLogAspect {

    /**
     * 以自定义@WebLog注解为切点
     */
    @Pointcut("@annotation(kr.weitao.starter.config.annotation.WebLog)")
    public void webLog() {
    }

    /**
     * 环绕
     */
    @Around("webLog()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        long startTime = System.currentTimeMillis();
        Object result = pjp.proceed();
        if (!(result instanceof ResponseEntity) && !(result instanceof BufferedImage)) {
            log.info("Response Args  : {}", new Gson().toJson(result));
            log.info("Time-Consuming : {}", System.currentTimeMillis() - startTime);
        }

        return result;
    }


    /**
     * 在切点之前记录
     */
    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Exception {
        // 开始打印请求日志
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = Objects.requireNonNull(attributes).getRequest();
        // 获取 @WebLog 注解的描述信息
        String methodDescription = getAspectLogDescription(joinPoint);
        // 打印请求相关参数
        log.info("======================================== Start ==========================================");
        // 打印请求 url
        log.info("URL            : {}", request.getRequestURL().toString());
        // 打印描述信息
        log.info("Description    : {}", methodDescription);
        // 打印 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   : {}", new Gson().toJson(joinPoint.getArgs()));
    }

    /**
     * 获取切面注解的描述
     *
     * @param joinPoint 切点
     * @return 描述信息
     * @throws Exception
     */
    public String getAspectLogDescription(JoinPoint joinPoint)
            throws Exception {
        String targetName = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();
        Object[] arguments = joinPoint.getArgs();
        Class targetClass = Class.forName(targetName);
        Method[] methods = targetClass.getMethods();
        StringBuilder description = new StringBuilder();
        for (Method method : methods) {
            if (method.getName().equals(methodName)) {
                Class[] clazzs = method.getParameterTypes();
                if (clazzs.length == arguments.length) {
                    description.append(method.getAnnotation(WebLog.class).description());
                    break;
                }
            }
        }
        return description.toString();
    }
}

相关文章

  • 通用的记录日志注解

    通过AOP定义通用的记录日志注解 需求: 实现AOP日记记录 定义日志注解 定义日志拦截器 可扩展的日志生成规则模...

  • SpringBoot-AOP

    SpringBoot-AOP 使用AOP统一处理请求日志 1.AOP的概念 AOP:AOP是一种编程范式,与语言无...

  • Spring Boot中使用log4j实现http请求日志入mo

    之前在《使用AOP统一处理Web请求日志》一文中介绍了如何使用AOP统一记录web请求日志。基本思路是通过aop去...

  • AOP、IOC

    AOP(Aspect Oriented Programming)面向切面编程 AOP主要用于解决横切关注点(日志、...

  • 基于注解的SpringAOP切面编程实例

    1,AOP简介 面相切面编程(AOP),可以实现例如日志打印、身份认证,权限管理,安全监测。AOP的实现原理是基于...

  • 结合实际研发经验讲解spring核心模块aop

    AOP:是Spring的AOP库,提供了拦截器的机制。 AOP面向切面编程,基于代理设计模式。 主要功能:日志记录...

  • Spring Boot 学习之路八,AOP统一处理请求日志

    AOP统一处理请求日志 AOP为Aspect Oriented Programming的缩写,意为:[面向切面编程...

  • spring AOP应用场景

    Spring Boot中使用AOP统一处理Web请求日志 AOP为Aspect Oriented Programm...

  • 8、AOP

    Spring AOP: spring分为:1、IOC/DI 2、AOPAOP的使用场景:日志和事务概念:AOP为A...

  • 每日一结——Spring AOP

    AOP简介 AOP将业务模块与周边功能或者说为业务模块服务的功能区分开,例如:权限控制、日志统计。AOP将这些共性...

网友评论

      本文标题:AOP日志

      本文链接:https://www.haomeiwen.com/subject/cwlmlktx.html