当前位置:Gxlcms > 数据库问题 > spring boot aop日志管理(MongoDB)

spring boot aop日志管理(MongoDB)

时间:2021-07-01 10:21:17 帮助过:14人阅读

2、代码实现

@Aspect
@Component
@Slf4j
public class LogAop {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Pointcut("execution(public * com.caody.muyi.controller.*.*(..))")
    public void logAop(){};

    @Before("logAop()")
    public void around(JoinPoint joinPoint){
        log.info("user:cdy");
        log.info("time:"+new Date());
        log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature
            ().getName());
        log.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));

        OperationLog operationLog = new OperationLog();
        operationLog.setLogname("cdy");
        operationLog.setLogtype("业务日志");
        operationLog.setCreatetime(new Date());
        operationLog.setUserid(1);
        operationLog.setClassname(joinPoint.getSignature().getDeclaringTypeName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setSucceed("成功");
        operationLog.setMessage("");
       mongoTemplate.save(operationLog);
    }

//    @AfterReturning(returning = "object", pointcut = "logAop()")
//    public void after(Object object){
//        System.out.println(object);
//        log.info("RESPONSE : " + object);
//    }

    @AfterThrowing(pointcut = "logAop()", throwing="e")
    public  void  afterThrowing(JoinPoint joinPoint, Throwable e){
        log.info("user:cdy");
        log.info("time:"+new Date());
        log.info("path : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature
            ().getName());
        log.info("param : " + Arrays.toString(joinPoint.getArgs()));
        log.info("异常代码:" + e.getClass().getName());
        log.info("异常信息:" + e.getMessage());

        OperationLog operationLog = new OperationLog();
        operationLog.setLogname("cdy");
        operationLog.setLogtype("异常日志");
        operationLog.setCreatetime(new Date());
        operationLog.setUserid(1);
        operationLog.setClassname(joinPoint.getSignature().getDeclaringTypeName());
        operationLog.setMethod(joinPoint.getSignature().getName());
        operationLog.setSucceed("失败");
        operationLog.setMessage(e.getMessage());
        mongoTemplate.save(operationLog);

    }

}


作者:周六不算加班
链接:https://www.jianshu.com/p/a0f3adced194
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

spring boot aop日志管理(MongoDB)

标签:owa   异常   失败   引用   eth   pen   date   lan   set   

人气教程排行