From 7c3f15a6f647afabb061e560ad3335d47806d33f Mon Sep 17 00:00:00 2001 From: liquor <958142070@qq.com> Date: Wed, 11 Dec 2024 09:03:09 +0000 Subject: [PATCH] =?UTF-8?q?feat(log/aop):=20=E6=96=B0=E5=A2=9E=20log-aop?= =?UTF-8?q?=20=E7=BB=84=E4=BB=B6=E6=A8=A1=E5=9D=97=EF=BC=88=E5=9F=BA?= =?UTF-8?q?=E4=BA=8E=20AOP=20=E5=AE=9E=E7=8E=B0=E6=97=A5=E5=BF=97=E8=AE=B0?= =?UTF-8?q?=E5=BD=95=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- continew-starter-dependencies/pom.xml | 10 +- .../continew-starter-log-aop/pom.xml | 21 ++ .../annotation}/ConditionalOnEnabledLog.java | 2 +- .../starter/log/aop/annotation/Log.java | 54 +++++ .../log/aop/aspect/ConsoleLogAspect.java | 80 +++++++ .../starter/log/aop/aspect/LogAspect.java | 221 ++++++++++++++++++ .../autoconfigure/LogAutoConfiguration.java | 87 +++++++ .../log/aop/autoconfigure/LogProperties.java | 76 ++++++ ...ot.autoconfigure.AutoConfiguration.imports | 1 + .../recordable}/RecordableHttpRequest.java | 2 +- .../recordable}/RecordableHttpResponse.java | 2 +- .../impl}/RecordableServletHttpRequest.java | 4 +- .../impl}/RecordableServletHttpResponse.java | 4 +- .../starter/log/core/model/LogRecord.java | 14 ++ .../starter/log/core/model/LogRequest.java | 1 + .../starter/log/core/model/LogResponse.java | 1 + .../annotation/ConditionalOnEnabledLog.java | 35 +++ .../log/interceptor}/annotation/Log.java | 2 +- .../autoconfigure/LogAutoConfiguration.java | 3 +- .../interceptor/handler/LogInterceptor.java | 4 +- continew-starter-log/pom.xml | 1 + 21 files changed, 614 insertions(+), 11 deletions(-) create mode 100644 continew-starter-log/continew-starter-log-aop/pom.xml rename continew-starter-log/{continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/autoconfigure => continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation}/ConditionalOnEnabledLog.java (95%) create mode 100644 continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation/Log.java create mode 100644 continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/aspect/ConsoleLogAspect.java create mode 100644 continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/aspect/LogAspect.java create mode 100644 continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/autoconfigure/LogAutoConfiguration.java create mode 100644 continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/autoconfigure/LogProperties.java create mode 100644 continew-starter-log/continew-starter-log-aop/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports rename continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/{model => http/recordable}/RecordableHttpRequest.java (96%) rename continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/{model => http/recordable}/RecordableHttpResponse.java (95%) rename continew-starter-log/{continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler => continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/impl}/RecordableServletHttpRequest.java (96%) rename continew-starter-log/{continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler => continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/impl}/RecordableServletHttpResponse.java (94%) create mode 100644 continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/annotation/ConditionalOnEnabledLog.java rename continew-starter-log/{continew-starter-log-core/src/main/java/top/continew/starter/log/core => continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor}/annotation/Log.java (96%) diff --git a/continew-starter-dependencies/pom.xml b/continew-starter-dependencies/pom.xml index 136f379b..1b8eeb59 100644 --- a/continew-starter-dependencies/pom.xml +++ b/continew-starter-dependencies/pom.xml @@ -490,6 +490,13 @@ ${revision} + + + top.continew + continew-starter-log-aop + ${revision} + + top.continew @@ -680,7 +687,8 @@ - ${project.build.directory}/effective-pom/continew-starter-dependencies.xml + ${project.build.directory}/effective-pom/continew-starter-dependencies.xml + effective-pom diff --git a/continew-starter-log/continew-starter-log-aop/pom.xml b/continew-starter-log/continew-starter-log-aop/pom.xml new file mode 100644 index 00000000..8d2caa3e --- /dev/null +++ b/continew-starter-log/continew-starter-log-aop/pom.xml @@ -0,0 +1,21 @@ + + + 4.0.0 + + top.continew + continew-starter-log + ${revision} + + continew-starter-log-aop + ContiNew Starter 日志模块 - 基于 AOP 实现日志记录 + + + + + + top.continew + continew-starter-log-core + + + diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/autoconfigure/ConditionalOnEnabledLog.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation/ConditionalOnEnabledLog.java similarity index 95% rename from continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/autoconfigure/ConditionalOnEnabledLog.java rename to continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation/ConditionalOnEnabledLog.java index 69e738c6..a0a973a5 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/autoconfigure/ConditionalOnEnabledLog.java +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation/ConditionalOnEnabledLog.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.log.interceptor.autoconfigure; +package top.continew.starter.log.aop.annotation; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import top.continew.starter.core.constant.PropertiesConstants; diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation/Log.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation/Log.java new file mode 100644 index 00000000..c0521fd9 --- /dev/null +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation/Log.java @@ -0,0 +1,54 @@ +/* + * 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.aop.annotation; + +import top.continew.starter.log.core.enums.Include; + +import java.lang.annotation.*; + +/** + * 日志注解 + *

用于接口方法或类上

+ * + * @author Charles7c + * @since 1.1.0 + */ +@Documented +@Target({ElementType.METHOD, ElementType.TYPE}) +@Retention(RetentionPolicy.RUNTIME) +public @interface Log { + + /** + * 所属模块(用于接口方法或类上) + */ + String module() default ""; + + /** + * 日志描述 - 接口操作内容(仅用于接口方法上) + */ + String value() default ""; + + /** + * 包含信息(在全局配置基础上扩展包含信息) + */ + Include[] includes() default {}; + + /** + * 排除信息(在全局配置基础上减少包含信息) + */ + Include[] excludes() default {}; +} diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/aspect/ConsoleLogAspect.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/aspect/ConsoleLogAspect.java new file mode 100644 index 00000000..696ac3a7 --- /dev/null +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/aspect/ConsoleLogAspect.java @@ -0,0 +1,80 @@ +package top.continew.starter.log.aop.aspect; + +import com.alibaba.ttl.TransmittableThreadLocal; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.aspectj.lang.annotation.After; +import org.aspectj.lang.annotation.Aspect; +import org.aspectj.lang.annotation.Before; +import org.aspectj.lang.annotation.Pointcut; +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.aop.autoconfigure.LogProperties; + +import java.time.Duration; +import java.time.Instant; + +/** + * 控制台 输出日志切面 + * + * @author echo + * @date 2024/12/06 10:33 + **/ +@Aspect +public class ConsoleLogAspect { + + private static final Logger log = LoggerFactory.getLogger(ConsoleLogAspect.class); + private final LogProperties logProperties; + private final TransmittableThreadLocal timeTtl = new TransmittableThreadLocal<>(); + + public ConsoleLogAspect(LogProperties logProperties) { + this.logProperties = logProperties; + } + + /** + * 切点 - 匹配所有控制器层的方法 + */ + @Pointcut("execution(* *..controller.*.*(..)) || execution(* *..*Controller.*(..))") + public void controllerLayer() { + } + + /** + * 处理请求前执行 + */ + @Before(value = "controllerLayer()") + public void doBefore() { + // 打印请求日志 + if (Boolean.TRUE.equals(logProperties.getIsPrint())) { + Instant startTime = Instant.now(); + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (attributes != null) { + HttpServletRequest request = attributes.getRequest(); + log.info("[{}] {}", request.getMethod(), request.getRequestURI()); + } + timeTtl.set(startTime); + } + } + + /** + * 处理请求后执行 + */ + @After(value = "controllerLayer()") + public void afterAdvice() { + // 打印请求耗时 + if (Boolean.TRUE.equals(logProperties.getIsPrint())) { + Instant endTime = Instant.now(); + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (attributes == null) { + return; + } + HttpServletRequest request = attributes.getRequest(); + HttpServletResponse response = attributes.getResponse(); + Duration timeTaken = Duration.between(timeTtl.get(), endTime); + log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), + response != null ? response.getStatus() : "N/A", + timeTaken.toMillis()); + } + } +} diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/aspect/LogAspect.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/aspect/LogAspect.java new file mode 100644 index 00000000..2ba06682 --- /dev/null +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/aspect/LogAspect.java @@ -0,0 +1,221 @@ +package top.continew.starter.log.aop.aspect; + +import cn.hutool.core.text.CharSequenceUtil; +import cn.hutool.core.util.StrUtil; +import com.alibaba.ttl.TransmittableThreadLocal; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.aspectj.lang.JoinPoint; +import org.aspectj.lang.annotation.*; +import org.aspectj.lang.reflect.MethodSignature; +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.aop.annotation.Log; +import top.continew.starter.log.aop.autoconfigure.LogProperties; +import top.continew.starter.log.core.dao.LogDao; +import top.continew.starter.log.core.enums.Include; +import top.continew.starter.log.core.http.recordable.impl.RecordableServletHttpRequest; +import top.continew.starter.log.core.http.recordable.impl.RecordableServletHttpResponse; +import top.continew.starter.log.core.model.LogRecord; + +import java.lang.reflect.Method; +import java.time.Instant; +import java.util.HashSet; +import java.util.Set; + +/** + * 日志切面 + * + * @author echo + * @date 2024/12/06 09:58 + **/ +@Aspect +public class LogAspect { + + private static final Logger log = LoggerFactory.getLogger(LogAspect.class); + private final LogDao logDao; + private final LogProperties logProperties; + private final TransmittableThreadLocal timeTtl = new TransmittableThreadLocal<>(); + private final TransmittableThreadLocal logTtl = new TransmittableThreadLocal<>(); + + public LogAspect(LogDao logDao, LogProperties logProperties) { + this.logDao = logDao; + this.logProperties = logProperties; + } + + /** + * 切点 - 匹配日志注解 {@link Log} + */ + @Pointcut(value = "@annotation(top.continew.starter.log.aop.annotation.Log)") + public void pointcutService() { + } + + /** + * 处理请求前执行 + */ + @Before(value = "pointcutService()") + public void doBefore() { + Instant startTime = Instant.now(); + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (attributes != null) { + HttpServletRequest request = attributes.getRequest(); + LogRecord.Started startedLogRecord = LogRecord.start(startTime, new RecordableServletHttpRequest(request)); + logTtl.set(startedLogRecord); + } + } + + + /** + * 处理请求后执行 - 正常返回 + * + * @param joinPoint 切点 + */ + @After(value = "pointcutService()") + public void afterAdvice(JoinPoint joinPoint) { + handleAfterCompletion(joinPoint, null); + } + + /** + * 处理请求后执行 - 异常情况 + * + * @param joinPoint 切点 + * @param ex 异常 + */ + @AfterThrowing(pointcut = "pointcutService()", throwing = "ex") + public void afterThrowing(JoinPoint joinPoint, Exception ex) { + handleAfterCompletion(joinPoint, ex); + } + + /** + * 处理请求完成后的逻辑 + * + * @param joinPoint 切点 + * @param ex 异常 + * @param result 返回结果 + */ + private void handleAfterCompletion(JoinPoint joinPoint, Exception ex) { + try { + Instant endTime = Instant.now(); + ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); + if (attributes == null) { + return; + } + HttpServletResponse response = attributes.getResponse(); + // 处理日志记录 + LogRecord.Started startedLogRecord = logTtl.get(); + if (startedLogRecord == null) { + return; + } + + // 获取方法和类注解信息 + MethodSignature signature = (MethodSignature) joinPoint.getSignature(); + Method method = signature.getMethod(); + Class targetClass = joinPoint.getTarget().getClass(); + + Log methodLog = method.getAnnotation(Log.class); + Log classLog = targetClass.getAnnotation(Log.class); + + // 获取日志包含信息 + Set includeSet = getIncludes(methodLog, classLog); + + // 完成日志记录 + LogRecord finishedLogRecord = startedLogRecord + .finish(endTime, + new RecordableServletHttpResponse(response, response.getStatus()), + includeSet); + // 记录异常 + if (ex != null) { + finishedLogRecord.getResponse().setStatus(1); + finishedLogRecord.setErrorMsg(StrUtil.sub(ex.getMessage(), 0, 2000)); + } + + // 记录日志描述 + if (includeSet.contains(Include.DESCRIPTION)) { + logDescription(finishedLogRecord, methodLog); + } + + // 记录所属模块 + if (includeSet.contains(Include.MODULE)) { + logModule(finishedLogRecord, methodLog, classLog); + } + logDao.add(finishedLogRecord); + } catch (Exception e) { + log.error("Logging http log occurred an error: {}.", e.getMessage(), e); + } finally { + timeTtl.remove(); + logTtl.remove(); + } + } + + /** + * 获取日志包含信息 + * + * @param methodLog 方法级 Log 注解 + * @param classLog 类级 Log 注解 + * @return 日志包含信息 + */ + private Set getIncludes(Log methodLog, Log classLog) { + Set includeSet = new HashSet<>(logProperties.getIncludes()); + if (null != classLog) { + processInclude(includeSet, classLog); + } + if (null != methodLog) { + processInclude(includeSet, methodLog); + } + return includeSet; + } + + /** + * 处理日志包含信息 + * + * @param includes 日志包含信息 + * @param logAnnotation Log 注解 + */ + private void processInclude(Set includes, Log logAnnotation) { + Include[] includeArr = logAnnotation.includes(); + if (includeArr.length > 0) { + includes.addAll(Set.of(includeArr)); + } + Include[] excludeArr = logAnnotation.excludes(); + if (excludeArr.length > 0) { + includes.removeAll(Set.of(excludeArr)); + } + } + + /** + * 记录描述 + * + * @param logRecord 日志信息 + * @param methodLog 方法级 Log 注解 + */ + private void logDescription(LogRecord logRecord, Log methodLog) { + // 如果方法注解存在日志描述 + if (null != methodLog && CharSequenceUtil.isNotBlank(methodLog.value())) { + logRecord.setDescription(methodLog.value()); + } else { + logRecord.setDescription("请在该接口方法上指定日志描述"); + } + } + + /** + * 记录模块 + * + * @param logRecord 日志信息 + * @param methodLog 方法级 Log 注解 + * @param classLog 类级 Log 注解 + */ + private void logModule(LogRecord logRecord, Log methodLog, Log classLog) { + // 优先使用方法注解的模块 + if (null != methodLog && CharSequenceUtil.isNotBlank(methodLog.module())) { + logRecord.setModule(methodLog.module()); + return; + } + + // 其次使用类注解的模块 + if (null != classLog) { + logRecord.setModule(CharSequenceUtil.blankToDefault(classLog.module(), "请在该接口类上指定所属模块")); + } + } +} diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/autoconfigure/LogAutoConfiguration.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/autoconfigure/LogAutoConfiguration.java new file mode 100644 index 00000000..16ce2f64 --- /dev/null +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/autoconfigure/LogAutoConfiguration.java @@ -0,0 +1,87 @@ +/* + * 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.aop.autoconfigure; + +import jakarta.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import top.continew.starter.log.aop.annotation.ConditionalOnEnabledLog; +import top.continew.starter.log.aop.aspect.ConsoleLogAspect; +import top.continew.starter.log.aop.aspect.LogAspect; +import top.continew.starter.log.core.dao.LogDao; +import top.continew.starter.log.core.dao.impl.LogDaoDefaultImpl; + +/** + * 日志自动配置 + * + * @author Charles7c + * @since 1.1.0 + */ +@Configuration +@ConditionalOnEnabledLog +@EnableConfigurationProperties(LogProperties.class) +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +public class LogAutoConfiguration { + + private static final Logger log = LoggerFactory.getLogger(LogAutoConfiguration.class); + private final LogProperties logProperties; + + public LogAutoConfiguration(LogProperties logProperties) { + this.logProperties = logProperties; + } + + /** + * 记录日志切面 + * + * @return {@link LogAspect } + */ + @Bean + @ConditionalOnMissingBean + public LogAspect logAspect() { + return new LogAspect(logDao(),logProperties); + } + + /** + * 控制台输出日志切面 + * + * @return {@link LogAspect } + */ + @Bean + @ConditionalOnMissingBean + public ConsoleLogAspect consoleLogAspect() { + return new ConsoleLogAspect(logProperties); + } + + /** + * 日志持久层接口 + */ + @Bean + @ConditionalOnMissingBean + public LogDao logDao() { + return new LogDaoDefaultImpl(); + } + + @PostConstruct + public void postConstruct() { + log.debug("[ContiNew Starter] - Auto Configuration 'Log-aop' completed initialization."); + } +} diff --git a/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/autoconfigure/LogProperties.java b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/autoconfigure/LogProperties.java new file mode 100644 index 00000000..4cbc1299 --- /dev/null +++ b/continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/autoconfigure/LogProperties.java @@ -0,0 +1,76 @@ +/* + * 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.aop.autoconfigure; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import top.continew.starter.core.constant.PropertiesConstants; +import top.continew.starter.log.core.enums.Include; + +import java.util.Set; + +/** + * 日志配置属性 + * + * @author Charles7c + * @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-aop/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/continew-starter-log/continew-starter-log-aop/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 00000000..5ee12e4c --- /dev/null +++ b/continew-starter-log/continew-starter-log-aop/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +top.continew.starter.log.aop.autoconfigure.LogAutoConfiguration \ No newline at end of file diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/RecordableHttpRequest.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/RecordableHttpRequest.java similarity index 96% rename from continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/RecordableHttpRequest.java rename to continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/RecordableHttpRequest.java index b5bd3899..914dbc0d 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/RecordableHttpRequest.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/RecordableHttpRequest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.log.core.model; +package top.continew.starter.log.core.http.recordable; import java.net.URI; import java.util.Map; diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/RecordableHttpResponse.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/RecordableHttpResponse.java similarity index 95% rename from continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/RecordableHttpResponse.java rename to continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/RecordableHttpResponse.java index a1ae77ee..28253ce4 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/RecordableHttpResponse.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/RecordableHttpResponse.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.log.core.model; +package top.continew.starter.log.core.http.recordable; import java.util.Map; diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/RecordableServletHttpRequest.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/impl/RecordableServletHttpRequest.java similarity index 96% rename from continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/RecordableServletHttpRequest.java rename to continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/impl/RecordableServletHttpRequest.java index 9e374691..775dbd0a 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/RecordableServletHttpRequest.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/impl/RecordableServletHttpRequest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.log.interceptor.handler; +package top.continew.starter.log.core.http.recordable.impl; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.StrUtil; @@ -25,7 +25,7 @@ import org.springframework.web.util.ContentCachingRequestWrapper; import org.springframework.web.util.UriUtils; import org.springframework.web.util.WebUtils; import top.continew.starter.core.constant.StringConstants; -import top.continew.starter.log.core.model.RecordableHttpRequest; +import top.continew.starter.log.core.http.recordable.RecordableHttpRequest; import java.net.URI; import java.net.URISyntaxException; diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/RecordableServletHttpResponse.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/impl/RecordableServletHttpResponse.java similarity index 94% rename from continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/RecordableServletHttpResponse.java rename to continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/impl/RecordableServletHttpResponse.java index 88ad5c57..c9cdccad 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/RecordableServletHttpResponse.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/http/recordable/impl/RecordableServletHttpResponse.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.log.interceptor.handler; +package top.continew.starter.log.core.http.recordable.impl; import cn.hutool.core.text.CharSequenceUtil; import cn.hutool.core.util.StrUtil; @@ -22,7 +22,7 @@ import cn.hutool.json.JSONUtil; import jakarta.servlet.http.HttpServletResponse; import org.springframework.web.util.ContentCachingResponseWrapper; import org.springframework.web.util.WebUtils; -import top.continew.starter.log.core.model.RecordableHttpResponse; +import top.continew.starter.log.core.http.recordable.RecordableHttpResponse; import top.continew.starter.web.util.ServletUtils; import java.util.Map; diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRecord.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRecord.java index 9df4a49f..988f13c1 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRecord.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRecord.java @@ -17,6 +17,8 @@ package top.continew.starter.log.core.model; import top.continew.starter.log.core.enums.Include; +import top.continew.starter.log.core.http.recordable.RecordableHttpRequest; +import top.continew.starter.log.core.http.recordable.RecordableHttpResponse; import java.time.Duration; import java.time.Instant; @@ -63,6 +65,11 @@ public class LogRecord { */ private final Instant timestamp; + /** + * 错误信息 + */ + private String errorMsg; + public LogRecord(Instant timestamp, LogRequest request, LogResponse response, Duration timeTaken) { this.timestamp = timestamp; this.request = request; @@ -164,4 +171,11 @@ public class LogRecord { public Instant getTimestamp() { return timestamp; } + + public String getErrorMsg() { + return errorMsg; + } + public void setErrorMsg(String errorMsg) { + this.errorMsg = errorMsg; + } } diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRequest.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRequest.java index a13c2c8c..2fdf58c2 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRequest.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogRequest.java @@ -21,6 +21,7 @@ import org.springframework.http.HttpHeaders; import top.continew.starter.core.util.ExceptionUtils; import top.continew.starter.core.util.IpUtils; import top.continew.starter.log.core.enums.Include; +import top.continew.starter.log.core.http.recordable.RecordableHttpRequest; import top.continew.starter.web.util.ServletUtils; import java.net.URI; diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogResponse.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogResponse.java index a022b7c6..cf6ef7bf 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogResponse.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/model/LogResponse.java @@ -17,6 +17,7 @@ package top.continew.starter.log.core.model; import top.continew.starter.log.core.enums.Include; +import top.continew.starter.log.core.http.recordable.RecordableHttpResponse; import java.util.Map; import java.util.Set; diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/annotation/ConditionalOnEnabledLog.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/annotation/ConditionalOnEnabledLog.java new file mode 100644 index 00000000..80246c8f --- /dev/null +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/annotation/ConditionalOnEnabledLog.java @@ -0,0 +1,35 @@ +/* + * 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.interceptor.annotation; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import top.continew.starter.core.constant.PropertiesConstants; + +import java.lang.annotation.*; + +/** + * 是否启用日志记录注解 + * + * @author Charles7c + * @since 1.1.0 + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ElementType.TYPE, ElementType.METHOD}) +@Documented +@ConditionalOnProperty(prefix = PropertiesConstants.LOG, name = PropertiesConstants.ENABLED, havingValue = "true", matchIfMissing = true) +public @interface ConditionalOnEnabledLog { +} \ No newline at end of file diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/annotation/Log.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/annotation/Log.java similarity index 96% rename from continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/annotation/Log.java rename to continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/annotation/Log.java index b8cef4fe..60bdff26 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/core/annotation/Log.java +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/annotation/Log.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package top.continew.starter.log.core.annotation; +package top.continew.starter.log.interceptor.annotation; import top.continew.starter.log.core.enums.Include; diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/autoconfigure/LogAutoConfiguration.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/autoconfigure/LogAutoConfiguration.java index d9934f09..87f284f9 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/autoconfigure/LogAutoConfiguration.java +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/autoconfigure/LogAutoConfiguration.java @@ -28,6 +28,7 @@ import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import top.continew.starter.log.core.dao.LogDao; import top.continew.starter.log.core.dao.impl.LogDaoDefaultImpl; +import top.continew.starter.log.interceptor.annotation.ConditionalOnEnabledLog; import top.continew.starter.log.interceptor.handler.LogFilter; import top.continew.starter.log.interceptor.handler.LogInterceptor; @@ -75,6 +76,6 @@ public class LogAutoConfiguration implements WebMvcConfigurer { @PostConstruct public void postConstruct() { - log.debug("[ContiNew Starter] - Auto Configuration 'Log' completed initialization."); + log.debug("[ContiNew Starter] - Auto Configuration 'Log-interceptor' completed initialization."); } } diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogInterceptor.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogInterceptor.java index 9a0c27ad..512dceb2 100644 --- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogInterceptor.java +++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/handler/LogInterceptor.java @@ -28,7 +28,9 @@ 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.core.annotation.Log; +import top.continew.starter.log.core.http.recordable.impl.RecordableServletHttpRequest; +import top.continew.starter.log.core.http.recordable.impl.RecordableServletHttpResponse; +import top.continew.starter.log.interceptor.annotation.Log; import top.continew.starter.log.core.dao.LogDao; import top.continew.starter.log.core.enums.Include; import top.continew.starter.log.core.model.LogRecord; diff --git a/continew-starter-log/pom.xml b/continew-starter-log/pom.xml index 561c4be2..2b7ec476 100644 --- a/continew-starter-log/pom.xml +++ b/continew-starter-log/pom.xml @@ -16,6 +16,7 @@ continew-starter-log-core continew-starter-log-interceptor + continew-starter-log-aop