+ * 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+ * 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+ * 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 @@