| | |
| | | * |
| | | * @param joinPoint 切点 |
| | | */ |
| | | @Around("logPointCut()") |
| | | public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable{ |
| | | SysUser user= SecurityUtils.getLoginUserWithoutError()!=null?SecurityUtils.getLoginUserWithoutError().getUser():null; |
| | | long startTime = System.currentTimeMillis(); |
| | | //获取当前请求对象 |
| | | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | HttpServletRequest request = attributes.getRequest(); |
| | | Signature signature = joinPoint.getSignature(); |
| | | MethodSignature methodSignature = (MethodSignature) signature; |
| | | Method method = methodSignature.getMethod(); |
| | | StringBuffer requestURL = request.getRequestURL(); |
| | | JSONObject webLog = new JSONObject(); |
| | | String urlStr = request.getRequestURL().toString(); |
| | | webLog.put("basePath", StringUtils.removeSuffix(urlStr, URLUtil.url(urlStr).getPath())); |
| | | webLog.put("ip", ServletUtil.getClientIP(request,null)); |
| | | webLog.put("method",request.getMethod()); |
| | | Object params=getParameter(method, joinPoint.getArgs()); |
| | | // @Around("logPointCut()") |
| | | // public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable{ |
| | | // |
| | | // SysUser user= SecurityUtils.getLoginUserWithoutError()!=null?SecurityUtils.getLoginUserWithoutError().getUser():null; |
| | | // long startTime = System.currentTimeMillis(); |
| | | // //获取当前请求对象 |
| | | // ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | // HttpServletRequest request = attributes.getRequest(); |
| | | // Signature signature = joinPoint.getSignature(); |
| | | // MethodSignature methodSignature = (MethodSignature) signature; |
| | | // Method method = methodSignature.getMethod(); |
| | | // StringBuffer requestURL = request.getRequestURL(); |
| | | // JSONObject webLog = new JSONObject(); |
| | | // String urlStr = request.getRequestURL().toString(); |
| | | // webLog.put("basePath", StringUtils.removeSuffix(urlStr, URLUtil.url(urlStr).getPath())); |
| | | // webLog.put("ip", ServletUtil.getClientIP(request,null)); |
| | | // webLog.put("method",request.getMethod()); |
| | | // Object params=getParameter(method, joinPoint.getArgs()); |
| | | // |
| | | // webLog.put("parameter",StringUtils.sub(JSON.toJSONString(params),0,2000)); |
| | | // webLog.put("uri",request.getRequestURI()); |
| | | // webLog.put("url",requestURL.toString()); |
| | | // if(user!=null) { |
| | | // webLog.put("userName", user.getName()); |
| | | // } |
| | | // log.info(webLog.toString()); |
| | | // Object result = joinPoint.proceed(); |
| | | // if (result == null) { |
| | | // //如果切到了 没有返回类型的void方法,这里直接返回 |
| | | // return null; |
| | | // } |
| | | // long endTime = System.currentTimeMillis(); |
| | | // webLog.put("result",StringUtils.sub(JSON.toJSONString(result),0,20000)); |
| | | // webLog.put("spendTime",endTime - startTime); |
| | | // log.info(webLog.toString()); |
| | | // return result; |
| | | // } |
| | | |
| | | webLog.put("parameter",StringUtils.sub(JSON.toJSONString(params),0,2000)); |
| | | webLog.put("uri",request.getRequestURI()); |
| | | webLog.put("url",requestURL.toString()); |
| | | if(user!=null) { |
| | | webLog.put("userName", user.getName()); |
| | | @Around("logPointCut()") |
| | | public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable { |
| | | // 初始化日志对象 |
| | | JSONObject webLog = new JSONObject(); |
| | | Object result = null; |
| | | long startTime = System.currentTimeMillis(); |
| | | |
| | | try { |
| | | // 1. 前置日志记录(单独try-catch防止此处异常影响主流程) |
| | | try { |
| | | SysUser user = SecurityUtils.getLoginUserWithoutError() != null |
| | | ? SecurityUtils.getLoginUserWithoutError().getUser() : null; |
| | | ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); |
| | | HttpServletRequest request = attributes.getRequest(); |
| | | |
| | | // 构建日志对象 |
| | | webLog.put("basePath", StringUtils.removeSuffix(request.getRequestURL().toString(), URLUtil.url(request.getRequestURL().toString()).getPath())); |
| | | webLog.put("ip", ServletUtil.getClientIP(request, null)); |
| | | webLog.put("method", request.getMethod()); |
| | | webLog.put("uri", request.getRequestURI()); |
| | | webLog.put("url", request.getRequestURL().toString()); |
| | | |
| | | // 方法参数记录 |
| | | Signature signature = joinPoint.getSignature(); |
| | | MethodSignature methodSignature = (MethodSignature) signature; |
| | | Object params = getParameter(methodSignature.getMethod(), joinPoint.getArgs()); |
| | | webLog.put("parameter", StringUtils.sub(JSON.toJSONString(params), 0, 2000)); |
| | | |
| | | if(user != null) { |
| | | webLog.put("userName", user.getName()); |
| | | } |
| | | log.info("[AOP-LOG] 请求日志: {}", webLog.toString()); |
| | | } catch (Exception preLogEx) { |
| | | log.error("[AOP-LOG] 前置日志记录异常", preLogEx); |
| | | } |
| | | |
| | | // 2. 核心业务执行(不catch,让业务异常正常抛出) |
| | | result = joinPoint.proceed(); |
| | | |
| | | } finally { |
| | | try { |
| | | // 3. 后置日志记录(确保始终执行,单独catch防止异常传播) |
| | | long endTime = System.currentTimeMillis(); |
| | | if(result != null) { |
| | | webLog.put("result", StringUtils.sub(JSON.toJSONString(result), 0, 20000)); |
| | | } |
| | | webLog.put("spendTime", endTime - startTime); |
| | | log.info("[AOP-LOG] 响应日志: {}", webLog.toString()); |
| | | } catch (Exception postLogEx) { |
| | | log.error("[AOP-LOG] 后置日志记录异常", postLogEx); |
| | | } |
| | | } |
| | | log.info(webLog.toString()); |
| | | Object result = joinPoint.proceed(); |
| | | if (result == null) { |
| | | //如果切到了 没有返回类型的void方法,这里直接返回 |
| | | return null; |
| | | } |
| | | long endTime = System.currentTimeMillis(); |
| | | webLog.put("result",StringUtils.sub(JSON.toJSONString(result),0,2000)); |
| | | webLog.put("spendTime",endTime - startTime); |
| | | log.info(webLog.toString()); |
| | | return result; |
| | | } |
| | | |