当前位置:首页 >> 安全

Java——AOP实现监控日志动态

来源:安全   2023年04月26日 12:22

//以批注MonitorLog标记的工具为更进一步 @Pointcut("@annotation(com.bbzd.mws.annotation.MonitorLog)") public void methodArgs(){} @Around("methodArgs()") public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable{ StringBuffer stringBuffer=new StringBuffer(); Object result=null; Stopwatch stopwatch = Stopwatch.createStarted(); HttpServletRequest httpServletRequest = ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest(); String ipAddr=getRemoteHost(httpServletRequest); String requestUrl=httpServletRequest.getRequestURI(); stringBuffer.append("请东光IP["+ipAddr+"];"); stringBuffer.append("请URL["+requestUrl+"];"); Signature signature=joinPoint.getSignature(); MethodSignature methodSignature=(MethodSignature)signature; // 类名 String[] sourceName = signature.getDeclaringTypeName().split("\."); String fullName=signature.getDeclaringTypeName(); String className = sourceName[sourceName.length - 1]; // 工具名 String methodName = signature.getName(); stringBuffer.append(""+className+"."+methodName+";"); // 数数值名数组 String[] parameterNames = methodSignature.getParameterNames(); Class[] parameterTypes=methodSignature.getParameterTypes(); // 内部结构数数值组交集 List argList = new ArrayList<>(); for (Object arg : joinPoint.getArgs()) { // request/response无法适用toJSON if (arg instanceof HttpServletRequest) { argList.add("request"); } else if (arg instanceof HttpServletResponse) { argList.add("response"); } else { argList.add(JSON.toJSON(arg)); } } stringBuffer.append("请数数值:"+JSON.toJSON(parameterNames)+"->"+JSON.toJSON(argList)); try{ result=joinPoint.proceed(); }catch(exception e){ stringBuffer.append("所致:"+e.getMessage()); //log.info("给与数数值失败:{}",e.getMessage()); } stopwatch.stop(); long timeConsuming = stopwatch.elapsed(TimeUnit.MILLISECONDS); if(result!=null){ stringBuffer.append("请结果:"+JSON.toJSON(result)); }else{ stringBuffer.append("请结果:无"); } stringBuffer.append("请耗时:"+timeConsuming+"毫秒"); Logger logger=getLogger(fullName,methodName,parameterTypes); logger.info(stringBuffer.toString()); return result; } /** * 从请中会给与请东光IP * @param request * @return 请东光IP */ private String getRemoteHost(HttpServletRequest request){ String ip = request.getHeader("x-forwarded-for"); if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ ip = request.getHeader("Proxy-Client-IP"); } if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ ip = request.getHeader("WL-Proxy-Client-IP"); } if(ip==null||ip.length()==0||"unknown".equalsIgnoreCase(ip)){ ip = request.getRemoteAddr(); } return ip.contains("0:0:0:0:0:0:0:1") ? "127.0.0.1" : ip; } /** * 根据MonitorLog批注中会的数值,赶回Logger * @param className MonitorLog所在工具相异的类名 * @param methodName MonitorLog所在工具相异的工具名 * @param paramTypes MonitorLog所在工具相异的数数值名 * @return */ private Logger getLogger( String className, String methodName, Class[] paramTypes){ String logName="com.bbzd.mws.aop"; try{ Class clazz=Class.forName(className); logName=clazz.getDeclaredMethod(methodName, paramTypes).getAnnotation(MonitorLog.class).value(); }catch(Exception e){ e.printStackTrace(); } Logger logger= LoggerFactory.getLogger(logName); return logger; }}4. 该公司逻辑工具@Override //com.bbzd.mws.aop是logger的重新命名,只能在快照副本中会进行时相异的配置@MonitorLog(value = "com.bbzd.mws.aop")public User getUserName(@Valid @RequestBody @WebParam() UserVo vo) throws ConstraintViolationException { User user = new User(vo.getName(), vo.getAge()); try{ //模拟所致情况,次测试所致信息的就有 //int i=1/0; }catch(NullPointerException exception){ exception.printStackTrace(); } return user;}5. 快照URL 6. 快照就有内容示例[11:11:59.266][INFO][com.bbzd.mws.aop][http-nio-8889-exec-1] 请东光IP[127.0.0.1];请URL[/mws/ws/user];UserServiceImpl.getUserName;请数数值:["vo"]->[{"name":"powerful","age":10}]请结果:{"name":"powerful","age":10}请耗时:56毫秒

总结

POM副本是示例片段URL是logback的示例片段其它副本是完整的示例关于logback快照框架及logbackURL的适用工具,后面会整理一篇社论详细讲解下,想知晓的小伙伴可以关切我。。

武汉妇科医院哪家专业
沈阳妇科医院哪家好点
石家庄治白癜风医院排名
济南妇科
南京肛肠医院怎么样
友情链接