使用AOP记录系统间接口日志

@Aspect
@Configuration
@Component
public class SynDataLogAop {

    private static Logger logger = Logger.getLogger(SynDataLogAop.class);

    private ExecutorService pool = Executors.newFixedThreadPool(20);

    @Pointcut("@annotation(com.yonyou.ocm.common.aop.TrackLog)")
    public void logPointcut(){
    }


    @Before("logPointcut()")
    public void outerLogin(JoinPoint joinPoint){
        try {
            LoginUserNameUtils.getInstance().loginSysUser();
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }


    @AfterReturning(returning = "result",pointcut = "logPointcut()")
    public void SynDataLog(JoinPoint joinPoint, HttpResult result){
        pool.execute(() -> {
            synchronized (this){
                try {
                    //校验方法的参数与返回值非空
                    SynchronousDataDto dto = this.checkNull(joinPoint, result);
                    if(dto.getIsSyn() == null ){
                        String name = ((MethodSignature)joinPoint.getSignature()).getMethod().getAnnotation(TrackLog.class).name();
                        if(StringUtils.isNotBlank(name)){
                            dto.setName(name);
                        }
                        //记录日志
                        this.recordLog(dto,result);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    logger.error(e.getMessage());
                }
            }
        });
    }
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TrackLog {

     String name() default "";
}

 

你可能感兴趣的:(SpringBoot)