当前位置: 代码迷 >> 综合 >> 通过注解 AOP 切面方法日志保存
  详细解决方案

通过注解 AOP 切面方法日志保存

热度:16   发布时间:2024-02-02 01:11:44.0
  /*** 切面PointCut*/@Pointcut("execution(@org.springframework.web.bind.annotation.RequestMapping * *(..))")public void  webLog(){};/*** 方法执行前执行* @param joinPoint* @throws Throwable*/@Before("webLog()")public  void doBefore(JoinPoint joinPoint) throws Throwable{try {LogInfo logInfo = new LogInfo();//获取登录信息UserVO user =userInfoManager.getCurrentUser();if(null!=user){logInfo.setLogUser(user.getName());//获取方法名String methodName = joinPoint.getSignature().getDeclaringTypeName()+"." + joinPoint.getSignature().getName();logInfo.setLogMethod(methodName);//获取请求地址HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();logInfo.setLogUrl(request.getRequestURL().toString());//传入参数json数据封装Map params = new HashMap<>(8);Object[] paramValues = joinPoint.getArgs();String[] paramNames = ((CodeSignature) joinPoint.getSignature()).getParameterNames();for (int i = 0; i < paramNames.length; i++) {String paramType = ((CodeSignature) joinPoint.getSignature()).getParameterTypes()[i].getName();//只保留String类型和int类型的参数if (paramType.indexOf("String") != -1 || paramType.indexOf("int") != -1) {params.put(paramNames[i], paramValues[i]);}}logInfo.setLogParams(JSONObject.toJSONString(params));if(logInfo.getLogParams().length()<250){logInfo.setLogType(Integer.parseInt(LogInfoTypeEnum.TYPE_INTERFACE.getCode()));logInfo.setTntInstId(CreditConstant.TNT_INST_ID);//保存到数据库logInfoRespository.save(logInfo);}else{log.info("请求参数过长,接口日志不异常");}}}catch (Exception e){log.error("接口日志保存异常",e);}}

 

  相关解决方案