1. 程式人生 > >使用AOP記錄系統間介面日誌

使用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 "";
}