From c5cb203532ea89b497121246f11ad858f1c3ac79 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Tue, 24 Dec 2024 21:50:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(log):=20=E4=BC=98=E5=8C=96=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/log/aspect/AccessLogAspect.java | 14 ++-- .../starter/log/aspect/LogAspect.java | 43 +++++++++-- .../autoconfigure/LogAutoConfiguration.java | 11 +++ .../log/autoconfigure/LogProperties.java | 76 ------------------- .../starter/log/handler/LogFilter.java | 2 +- .../starter/log/model}/LogProperties.java | 2 +- .../autoconfigure/LogAutoConfiguration.java | 1 + .../log/interceptor/LogInterceptor.java | 2 +- 8 files changed, 57 insertions(+), 94 deletions(-) delete mode 100644 continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogProperties.java rename continew-starter-log/{continew-starter-log-interceptor => continew-starter-log-core}/src/main/java/top/continew/starter/log/handler/LogFilter.java (98%) rename continew-starter-log/{continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure => continew-starter-log-core/src/main/java/top/continew/starter/log/model}/LogProperties.java (98%) diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/AccessLogAspect.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/AccessLogAspect.java index 5b54b694..86f7f1a0 100644 --- a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/AccessLogAspect.java +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/AccessLogAspect.java @@ -26,7 +26,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -import top.continew.starter.log.autoconfigure.LogProperties; +import top.continew.starter.log.model.LogProperties; import java.time.Duration; import java.time.Instant; @@ -51,42 +51,42 @@ public class AccessLogAspect { /** * 切点 - 匹配所有控制器层的 GET 请求方法 */ - @Pointcut("within(@org.springframework.web.bind.annotation.RequestMapping *)") + @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") public void pointcut() { } /** * 切点 - 匹配所有控制器层的 GET 请求方法 */ - @Pointcut("within(@org.springframework.web.bind.annotation.GetMapping *)") + @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)") public void pointcutGet() { } /** * 切点 - 匹配所有控制器层的 POST 请求方法 */ - @Pointcut("within(@org.springframework.web.bind.annotation.PostMapping *)") + @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)") public void pointcutPost() { } /** * 切点 - 匹配所有控制器层的 PUT 请求方法 */ - @Pointcut("within(@org.springframework.web.bind.annotation.PutMapping *)") + @Pointcut("@annotation(org.springframework.web.bind.annotation.PutMapping)") public void pointcutPut() { } /** * 切点 - 匹配所有控制器层的 DELETE 请求方法 */ - @Pointcut("within(@org.springframework.web.bind.annotation.DeleteMapping *)") + @Pointcut("@annotation(org.springframework.web.bind.annotation.DeleteMapping)") public void pointcutDelete() { } /** * 切点 - 匹配所有控制器层的 PATCH 请求方法 */ - @Pointcut("within(@org.springframework.web.bind.annotation.PatchMapping *)") + @Pointcut("@annotation(org.springframework.web.bind.annotation.PatchMapping)") public void pointcutPatch() { } 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 4cd4cbd7..149ea807 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,6 +16,7 @@ package top.continew.starter.log.aspect; +import cn.hutool.core.annotation.AnnotationUtil; import cn.hutool.core.text.CharSequenceUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -30,10 +31,11 @@ import org.slf4j.LoggerFactory; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import top.continew.starter.log.annotation.Log; -import top.continew.starter.log.autoconfigure.LogProperties; import top.continew.starter.log.dao.LogDao; import top.continew.starter.log.handler.LogHandler; +import top.continew.starter.log.model.LogProperties; import top.continew.starter.log.model.LogRecord; +import top.continew.starter.web.util.SpringWebUtils; import java.lang.reflect.Method; import java.time.Instant; @@ -76,13 +78,13 @@ public class LogAspect { @Around("pointcut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { Instant startTime = Instant.now(); - // 非 Web 环境不记录 - ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); - if (attributes == null || attributes.getResponse() == null) { + // 指定规则不记录 + HttpServletRequest request = SpringWebUtils.getRequest(); + Method targetMethod = this.getMethod(joinPoint); + Class targetClass = joinPoint.getTarget().getClass(); + if (!isRequestRecord(targetMethod, targetClass)) { return joinPoint.proceed(); } - HttpServletRequest request = attributes.getRequest(); - HttpServletResponse response = attributes.getResponse(); String errorMsg = null; // 开始记录 LogRecord.Started startedLogRecord = logHandler.start(startTime, request); @@ -95,8 +97,7 @@ public class LogAspect { } finally { try { Instant endTime = Instant.now(); - Method targetMethod = this.getMethod(joinPoint); - Class targetClass = joinPoint.getTarget().getClass(); + HttpServletResponse response = SpringWebUtils.getResponse(); LogRecord logRecord = logHandler.finish(startedLogRecord, endTime, response, logProperties .getIncludes(), targetMethod, targetClass); // 记录异常信息 @@ -111,6 +112,32 @@ public class LogAspect { } } + /** + * 是否要记录日志 + * + * @param targetMethod 目标方法 + * @param targetClass 目标类 + * @return true:需要记录;false:不需要记录 + */ + private boolean isRequestRecord(Method targetMethod, Class targetClass) { + // 非 Web 环境不记录 + ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); + if (attributes == null || attributes.getResponse() == null) { + return false; + } + // 如果接口匹配排除列表,不记录日志 + 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(); + } + /** * 获取方法 * diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java index 947d2e66..f3e28c67 100644 --- a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java @@ -30,7 +30,9 @@ import top.continew.starter.log.aspect.LogAspect; import top.continew.starter.log.dao.LogDao; import top.continew.starter.log.dao.impl.DefaultLogDaoImpl; import top.continew.starter.log.handler.AopLogHandler; +import top.continew.starter.log.handler.LogFilter; import top.continew.starter.log.handler.LogHandler; +import top.continew.starter.log.model.LogProperties; /** * 日志自动配置 @@ -52,6 +54,15 @@ public class LogAutoConfiguration { this.logProperties = logProperties; } + /** + * 日志过滤器 + */ + @Bean + @ConditionalOnMissingBean + public LogFilter logFilter() { + return new LogFilter(logProperties); + } + /** * 日志切面 * diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogProperties.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogProperties.java deleted file mode 100644 index a168fe7a..00000000 --- a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogProperties.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. - *

- * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *

- * http://www.gnu.org/licenses/lgpl.html - *

- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package top.continew.starter.log.autoconfigure; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import top.continew.starter.core.constant.PropertiesConstants; -import top.continew.starter.log.enums.Include; - -import java.util.Set; - -/** - * 日志配置属性 - * - * @author Charles7c - * @author echo - * @since 1.1.0 - */ -@ConfigurationProperties(PropertiesConstants.LOG) -public class LogProperties { - - /** - * 是否启用日志 - */ - private boolean enabled = true; - - /** - * 是否打印日志,开启后可打印访问日志(类似于 Nginx access log) - *

- * 不记录日志也支持开启打印访问日志 - *

- */ - private Boolean isPrint = false; - - /** - * 包含信息 - */ - private Set includes = Include.defaultIncludes(); - - public boolean isEnabled() { - return enabled; - } - - public void setEnabled(boolean enabled) { - this.enabled = enabled; - } - - public Boolean getIsPrint() { - return isPrint; - } - - public void setIsPrint(Boolean print) { - isPrint = print; - } - - public Set getIncludes() { - return includes; - } - - public void setIncludes(Set includes) { - this.includes = includes; - } -} diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/handler/LogFilter.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/LogFilter.java similarity index 98% rename from continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/handler/LogFilter.java rename to continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/LogFilter.java index cae13ac9..47ac36dc 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/handler/LogFilter.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/LogFilter.java @@ -28,7 +28,7 @@ import org.springframework.web.filter.OncePerRequestFilter; import org.springframework.web.util.ContentCachingRequestWrapper; import org.springframework.web.util.ContentCachingResponseWrapper; import org.springframework.web.util.WebUtils; -import top.continew.starter.log.autoconfigure.LogProperties; +import top.continew.starter.log.model.LogProperties; import java.io.IOException; import java.net.URI; diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogProperties.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogProperties.java similarity index 98% rename from continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogProperties.java rename to continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogProperties.java index ce497392..977e2caf 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogProperties.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/model/LogProperties.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.log.autoconfigure; +package top.continew.starter.log.model; import org.springframework.boot.context.properties.ConfigurationProperties; import top.continew.starter.core.constant.PropertiesConstants; diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java index d3a917be..77fc5749 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java @@ -33,6 +33,7 @@ import top.continew.starter.log.handler.InterceptorLogHandler; import top.continew.starter.log.handler.LogFilter; import top.continew.starter.log.handler.LogHandler; import top.continew.starter.log.interceptor.LogInterceptor; +import top.continew.starter.log.model.LogProperties; /** * 日志自动配置 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 73b633f9..cc315571 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 @@ -27,7 +27,7 @@ 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.autoconfigure.LogProperties; +import top.continew.starter.log.model.LogProperties; import top.continew.starter.log.dao.LogDao; import top.continew.starter.log.handler.LogHandler; import top.continew.starter.log.model.LogRecord;