diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java index bdf158ba..674affa3 100644 --- a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java @@ -16,7 +16,6 @@ package top.continew.starter.log.aspect; -import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.text.CharSequenceUtil; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; @@ -78,7 +77,7 @@ public class LogAspect { // 指定规则不记录 Method targetMethod = this.getMethod(joinPoint); Class targetClass = joinPoint.getTarget().getClass(); - if (!isRequestRecord(targetMethod, targetClass)) { + if (!isRecord(targetMethod, targetClass)) { return joinPoint.proceed(); } String errorMsg = null; @@ -108,13 +107,13 @@ public class LogAspect { } /** - * 是否要记录日志 + * 是否记录日志 * * @param targetMethod 目标方法 * @param targetClass 目标类 * @return true:需要记录;false:不需要记录 */ - private boolean isRequestRecord(Method targetMethod, Class targetClass) { + private boolean isRecord(Method targetMethod, Class targetClass) { // 非 Web 环境不记录 ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); if (attributes == null || attributes.getResponse() == null) { @@ -124,13 +123,7 @@ public class LogAspect { if (logProperties.isMatch(attributes.getRequest().getRequestURI())) { return false; } - // 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志 - Log methodLog = AnnotationUtil.getAnnotation(targetMethod, Log.class); - if (null != methodLog && methodLog.ignore()) { - return false; - } - Log classLog = AnnotationUtil.getAnnotation(targetClass, Log.class); - return null == classLog || !classLog.ignore(); + return logHandler.isRecord(targetMethod, targetClass); } /** diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java index fd560adb..e1a25077 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java @@ -36,11 +36,6 @@ import java.net.URISyntaxException; /** * 日志过滤器 * - * @author Dave Syer(Spring Boot Actuator) - * @author Wallace Wadge(Spring Boot Actuator) - * @author Andy Wilkinson(Spring Boot Actuator) - * @author Venil Noronha(Spring Boot Actuator) - * @author Madhura Bhave(Spring Boot Actuator) * @author Charles7c * @author echo * @since 1.1.0 diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java index 7d64aad9..ac0807fa 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java @@ -20,6 +20,7 @@ import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.ObjectUtil; import com.alibaba.ttl.TransmittableThreadLocal; +import io.swagger.v3.oas.annotations.Hidden; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.tags.Tag; import org.slf4j.Logger; @@ -49,6 +50,29 @@ public abstract class AbstractLogHandler implements LogHandler { private static final Logger log = LoggerFactory.getLogger(AbstractLogHandler.class); private final TransmittableThreadLocal logContextThread = new TransmittableThreadLocal<>(); + @Override + public boolean isRecord(Method targetMethod, Class targetClass) { + // 如果接口被隐藏,不记录日志 + Operation methodOperation = AnnotationUtil.getAnnotation(targetMethod, Operation.class); + if (null != methodOperation && methodOperation.hidden()) { + return false; + } + Hidden methodHidden = AnnotationUtil.getAnnotation(targetMethod, Hidden.class); + if (null != methodHidden) { + return false; + } + if (null != targetClass.getDeclaredAnnotation(Hidden.class)) { + return false; + } + // 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志 + Log methodLog = AnnotationUtil.getAnnotation(targetMethod, Log.class); + if (null != methodLog && methodLog.ignore()) { + return false; + } + Log classLog = AnnotationUtil.getAnnotation(targetClass, Log.class); + return null == classLog || !classLog.ignore(); + } + @Override public LogRecord.Started start(Instant startTime) { return LogRecord.start(startTime); diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/LogHandler.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/LogHandler.java index 8e1a3583..37e0a053 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/LogHandler.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/LogHandler.java @@ -33,6 +33,15 @@ import java.util.Set; */ public interface LogHandler { + /** + * 是否记录日志 + * + * @param targetMethod 目标方法 + * @param targetClass 目标类 + * @return 是否记录日志 + */ + boolean isRecord(Method targetMethod, Class targetClass); + /** * 开始日志记录 * diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java index f02b1ae7..9b58d3db 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java @@ -17,8 +17,6 @@ package top.continew.starter.log.interceptor; import com.alibaba.ttl.TransmittableThreadLocal; -import io.swagger.v3.oas.annotations.Hidden; -import io.swagger.v3.oas.annotations.Operation; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.slf4j.Logger; @@ -26,7 +24,6 @@ import org.slf4j.LoggerFactory; import org.springframework.lang.NonNull; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.HandlerInterceptor; -import top.continew.starter.log.annotation.Log; import top.continew.starter.log.dao.LogDao; import top.continew.starter.log.handler.LogHandler; import top.continew.starter.log.model.AccessLogContext; @@ -48,7 +45,6 @@ public class LogInterceptor implements HandlerInterceptor { private final LogProperties logProperties; private final LogHandler logHandler; private final LogDao logDao; - private final TransmittableThreadLocal timeTtl = new TransmittableThreadLocal<>(); private final TransmittableThreadLocal logTtl = new TransmittableThreadLocal<>(); public LogInterceptor(LogProperties logProperties, LogHandler logHandler, LogDao logDao) { @@ -64,7 +60,7 @@ public class LogInterceptor implements HandlerInterceptor { Instant startTime = Instant.now(); logHandler.accessLogStart(AccessLogContext.builder().startTime(startTime).properties(logProperties).build()); // 开始日志记录 - if (this.isRequestRecord(handler, request)) { + if (this.isRecord(handler)) { LogRecord.Started startedLogRecord = logHandler.start(startTime); logTtl.set(startedLogRecord); } @@ -94,40 +90,20 @@ public class LogInterceptor implements HandlerInterceptor { log.error("Logging http log occurred an error: {}.", ex.getMessage(), ex); throw ex; } finally { - timeTtl.remove(); logTtl.remove(); } } /** - * 是否要记录日志 + * 是否记录日志 * * @param handler 处理器 * @return true:需要记录;false:不需要记录 */ - private boolean isRequestRecord(Object handler, HttpServletRequest request) { + private boolean isRecord(Object handler) { if (!(handler instanceof HandlerMethod handlerMethod)) { return false; } - // 如果接口被隐藏,不记录日志 - Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class); - if (null != methodOperation && methodOperation.hidden()) { - return false; - } - Hidden methodHidden = handlerMethod.getMethodAnnotation(Hidden.class); - if (null != methodHidden) { - return false; - } - Class handlerBeanType = handlerMethod.getBeanType(); - if (null != handlerBeanType.getDeclaredAnnotation(Hidden.class)) { - return false; - } - // 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志 - Log methodLog = handlerMethod.getMethodAnnotation(Log.class); - if (null != methodLog && methodLog.ignore()) { - return false; - } - Log classLog = handlerBeanType.getDeclaredAnnotation(Log.class); - return null == classLog || !classLog.ignore(); + return logHandler.isRecord(handlerMethod.getMethod(), handlerMethod.getBeanType()); } }