From 6ccd09a395797b4e161889c6f4b30d3760ad2d2a Mon Sep 17 00:00:00 2001 From: zhouz <> Date: Tue, 30 Jul 2024 14:58:23 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=96=B0=E5=A2=9E=E6=89=93=E5=8D=B0?= =?UTF-8?q?=E9=BB=91=E5=90=8D=E5=8D=95=EF=BC=8C=E4=BB=A5=E5=8F=8A=E5=AE=9A?= =?UTF-8?q?=E6=97=B6=E5=99=A8=E6=97=A5=E5=BF=97=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../logging/aspect/WhiteListLogAspect.java | 49 +++++++------------ .../impl/SysInterfaceLogServiceImpl.java | 2 - .../src/main/resources/config/application.yml | 6 +-- 3 files changed, 19 insertions(+), 38 deletions(-) diff --git a/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/WhiteListLogAspect.java b/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/WhiteListLogAspect.java index ebe2c9c8a..f6eef4599 100644 --- a/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/WhiteListLogAspect.java +++ b/lms/nladmin-system/src/main/java/org/nl/modules/logging/aspect/WhiteListLogAspect.java @@ -1,5 +1,6 @@ package org.nl.modules.logging.aspect; +import cn.hutool.core.util.ObjectUtil; import cn.hutool.json.JSONUtil; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; @@ -62,7 +63,11 @@ public class WhiteListLogAspect { result = joinPoint.proceed(args); long endTime = System.currentTimeMillis(); long time = endTime - startTime; - addLog(joinPoint, JSONUtil.toJsonStr(result), time); + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + //黑名单接口不打印日志 + if (!shouldLog(signature.getName())){ + addLog(joinPoint, JSONUtil.toJsonStr(result), time); + } } catch (Exception e) { log.error("doAround日志记录异常,异常信息为:", e); throw e; @@ -75,39 +80,15 @@ public class WhiteListLogAspect { */ public void addLog(ProceedingJoinPoint joinPoint, String outParams, long time) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); - HttpServletRequest request = attributes.getRequest(); - HttpServletResponse response = attributes.getResponse(); - MethodSignature signature = (MethodSignature) joinPoint.getSignature(); - Method method = signature.getMethod(); + HttpServletRequest request = null; + if (ObjectUtil.isNotEmpty(attributes)) { + request = attributes.getRequest(); + } // 方法路径 - String methodName = joinPoint.getTarget().getClass().getName() + "." + signature.getName() + "()"; - String[] split = joinPoint.getTarget().getClass().getName().split("\\."); - String className = split[split.length - 1]; - StringUtils.getIp(request); - log.info("\n\r=======================================\n\r" + - "请求地址:{} \n\r" + - "请求方式:{} \n\r" + - "请求类方法:{} \n\r" + - "方法名::{} \n\r" + - "调用IP::{} \n\r" + - "接口名称:{} \n\r" + - "请求方法参数:{} \n\r" + - "返回报文:{} \n\r" + - "处理耗时:{} ms \n\r" + - "=======================================\n\r", - request.getRequestURI(), - request.getMethod(), - joinPoint.getSignature(), - className, - StringUtils.getIp(request), - signature.getName(), - JSONUtil.toJsonStr(filterArgs(joinPoint.getArgs())), - outParams, - time - ); SysInterfaceLog log = new SysInterfaceLog("INFO", time); - - interfaceLogService.save(getUsername(), outParams, StringUtils.getBrowser(request), StringUtils.getIp(request), joinPoint, log); + String borwser = ObjectUtil.isNotEmpty(attributes) ? StringUtils.getBrowser(request) : "定时器"; + String ip = ObjectUtil.isNotEmpty(attributes) ? StringUtils.getIp(request) : "定时器"; + interfaceLogService.save(getUsername(), outParams, borwser, ip, joinPoint, log); } public String getUsername() { @@ -138,5 +119,9 @@ public class WhiteListLogAspect { && !(object instanceof HttpServletResponse) ).collect(Collectors.toList()); } + + private boolean shouldLog(String requestURI) { + return loggingProperties.getIncludePaths().stream().anyMatch(pattern -> pathMatcher.match(pattern, requestURI)); + } } diff --git a/lms/nladmin-system/src/main/java/org/nl/system/service/logging/impl/SysInterfaceLogServiceImpl.java b/lms/nladmin-system/src/main/java/org/nl/system/service/logging/impl/SysInterfaceLogServiceImpl.java index 4ce771974..eb377b94c 100644 --- a/lms/nladmin-system/src/main/java/org/nl/system/service/logging/impl/SysInterfaceLogServiceImpl.java +++ b/lms/nladmin-system/src/main/java/org/nl/system/service/logging/impl/SysInterfaceLogServiceImpl.java @@ -39,8 +39,6 @@ public class SysInterfaceLogServiceImpl extends ServiceImpl