欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring boot Aop 日志注解

程序员文章站 2022-04-25 20:06:11
...

1.AopLog.java

package com.ptac.b2b.services.marketingcenter.business.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * @author  框架日志标识
 */
@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.METHOD, ElementType.TYPE })
public @interface AopLog {
}

2.AopLogAspect.java

package com.ptac.b2b.services.marketingcenter.business.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;

@Aspect
@Component
public class AopLogAspect {
    private static Logger logger = LoggerFactory.getLogger(AopLogAspect.class);


    /**
     * 类级别
     */
    @Pointcut("@within(com.ptac.b2b.services.marketingcenter.business.annotation.AopLog)")
    public void classCut(){

    }

    /**
     * 方法级别
     */
    @Pointcut("@annotation(com.ptac.b2b.services.marketingcenter.business.annotation.AopLog)")
    public void methodCut() {
    }

    @Pointcut("classCut() || methodCut()")
    public void pointCut(){
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint){
        //获取目标方法的参数信息
        Object[] obj = joinPoint.getArgs();
        Signature signature = joinPoint.getSignature();
        //代理的是什么类的哪一个方法
        logger.info("类:{} 方法: {} 开始",signature.getDeclaringTypeName() ,signature.getName());

        //AOP代理类的类(class)信息
        signature.getDeclaringType();
        MethodSignature methodSignature = (MethodSignature) signature;
        String[] strings = methodSignature.getParameterNames();
        logger.info("参数名: {}" , Arrays.toString(strings));
        logger.info("参数值: {}" , Arrays.toString(joinPoint.getArgs()));
    }

    // 执行成功无异常
    @AfterReturning(pointcut = "pointCut()", returning = "returnVal")
    public void afterReturning(JoinPoint joinPoint, Object returnVal) {
        Signature signature = joinPoint.getSignature();
        logger.info("返回值: {}" , returnVal);
        logger.info("类:{} 方法: {} 结束",signature.getDeclaringTypeName() ,signature.getName());
    }

    // 执行失败抛出异常
    @AfterThrowing(pointcut = "pointCut()", throwing = "e")
    public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
        Signature signature = joinPoint.getSignature();
        logger.info("异常: {}" , getStackTrace(e));
        logger.info("类:{} 方法: {} 结束",signature.getDeclaringTypeName() ,signature.getName());
    }

    public static String getStackTrace(Throwable throwable) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        throwable.printStackTrace(pw);
        return sw.getBuffer().toString();
    }

}

 

相关标签: Java Aop java