Java AOP记录方法信息以及存入Oracle的一些坑(ORA-01858和ORA-01843)

需要用AOP记录方法,大概效果如下:

 

@Aspect //切面  定义了通知和窃电的关系
@Component
@Slf4j
public class LogAspect {
    public MonitorDto monitorDto = new MonitorDto();
    
    @Resource
    MonitorService monitorService;
    @Pointcut("@annotation(com.abcd.acbdef.common.aop.LogMonitorRecord)")
    public void logPointCut() {
    }
    public MonitorDto getMonitorDto() {
        return this.monitorDto;
    }

    //通知类,标识为切点 环绕通知
    @Around("logPointCut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        long beginTime = System.currentTimeMillis();
        //执行方法
        Result result = (Result) point.proceed();
        Exception exception = null;
        try {
            result = (Result) point.proceed();
        } catch (Exception ex) {
            exception = ex;
        }
        //执行时长(毫秒)
        long time = System.currentTimeMillis() - beginTime;
        //保存日志
        recordLog(point, result, exception, time);
        if (exception != null) {
            throw exception;
        }
        return result;
    }
    private void recordLog(ProceedingJoinPoint joinPoint, Result result, Exception exception, long time) throws ParseException {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        LogMonitorRecord logAnnotation = method.getAnnotation(LogMonitorRecord.class);
        log.info("=================================log start============================================");
        log.info("module:{}",logAnnotation.module());
        log.info("operation:{}",logAnnotation.operator());
        monitorDto.setApiName(logAnnotation.operator());
        //请求的方法名
        String className = joinPoint.getTarget().getClass().getName();
        log.info("className:{}",className);
        //调用的接口路径
        monitorDto.setApiPathAndMethod(className);
        String methodName = signature.getName();
        //调用的方法名
        log.info("methodName:{}",methodName);
        log.info("request method:{}",className + "." + methodName + "()");

        DateTime now = DateTime.now();
        Timestamp formattedDate = now.toTimestamp();
        log.info("request time: {}", formattedDate);
        monitorDto.setRequestTime(formattedDate);

//        //请求的参数
//        Object[] args = joinPoint.getArgs();
//        String params = JSON.toJSONString(args[0]);
//        log.info("params:{}",params);
        //获取request 设置IP地址
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        log.info("ip:{}", IpUtils.getIpAddr(request));
        monitorDto.setRequestingIpAddress(IpUtils.getIpAddr(request));
        //执行时间
        log.info("excute time : {} ms",time);
        monitorDto.setCallDuration(String.valueOf(time));

        if (exception != null) {
            log.error("exception: {}", exception.getMessage());
            monitorDto.setExceptionInformation(exception.getMessage());
        } else {
            log.info("result: {}", result);  // 记录方法的结果
            // 假设你有一个方法来从结果对象中提取和记录返回码
            log.info("return code: {}", extractReturnCode(result));
            monitorDto.setExceptionInformation(result.toString());
        }
        log.info("==================================log end=============================================");
        try {
            int res = monitorService.updateMonitorAPIRecords(monitorDto);
            log.info("存入{}条接口操作记录。", res);

        }catch (Exception e) {
            log.error("记录接口调用记录:失败");
            log.error(e.getMessage());
        }
    }
    // 这是一个占位方法,你需要根据实际结果对象的结构来实现它
    private String extractReturnCode(Result result) {
        // 从结果对象中提取并返回返回码
        // 例如,如果结果是一个Map或自定义类,请访问返回码字段
        String rescode = result.getCode().toString();
        monitorDto.setResponseStatusCode(rescode);
        return rescode;  // 用实际的返回码提取逻辑替换这里
    }
}

还要对这个记录进行入库操作。
由于记录日志是在方法结束之后才能进行,所以不能在控制器方法入库,只能注解里提前写好了。
不过入库的时候会存在日期冲突的问题。
在向Oracle传入日期格式的数据时,如果出现ORA-18043(无效的月份)
或者ORA-18054(非数字数据)

基本都是date格式有问题。

我这里Oracle的格式为timestamp。

使用java.util.date 或者java.sql.date等等都有可能出现日期和你Oracle的那个格式对不上的问题,直接确定你Oracle的格式提前设置好就可以。

 

DateTime now = DateTime.now();
        Timestamp formattedDate = now.toTimestamp();
        log.info("request time: {}", formattedDate);

提前转化好日期就可以大概率规避掉这个问题。

注:本文的AOP参考了码神之路的博客教学

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值