AopLogger.java 2.06 KB
package pwc.taxtech.atms.common;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

    @Pointcut("execution(public * pwc.taxtech.atms.service.*.*(..))")
    public void pointCut() {
        // 定义Pointcut 此方法不能有返回值,该方法只是一个标示
    }

    @Before("pointCut()")
    public void before(JoinPoint joinPoint) {
        if (logger.isDebugEnabled()) {
            logger.debug("aop before:{}", joinPoint);
        }
    }

    @After("pointCut()")
    public void after(JoinPoint joinPoint) {
        if (logger.isDebugEnabled()) {
            logger.debug("aop after:{}", joinPoint);
        }
    }

    @AfterThrowing(pointcut = "pointCut()", throwing = "error")
    public void afterThrowing(JoinPoint joinPoint, Throwable error) {
        if (logger.isWarnEnabled()) {
            logger.warn("aop afterThrowing:{}, exception class:{}, exception message:{} ", joinPoint,
                    error.getClass().toString(), error.getMessage());
        }
    }

    // @After("pointCut()")
    // public void doAfter(JoinPoint joinPoint) {
    // System.out.println("AOP After Advice...");
    // }
    //
    // @AfterReturning(pointcut = "pointCut()", returning = "returnVal")
    // public void afterReturn(JoinPoint joinPoint, Object returnVal) {
    // System.out.println("AOP AfterReturning Advice:" + returnVal);
    // }
    //
    //
    // @Around("pointCut()")
    // public void around(ProceedingJoinPoint pjp) {
    // System.out.println("AOP Aronud before...");
    // try {
    // pjp.proceed();
    // } catch (Throwable e) {
    // e.printStackTrace();
    // }
    // System.out.println("AOP Aronud after...");
    // }

}