refactor(log): 优化基于 AOP 实现日志代码,调整日志整体包结构

This commit is contained in:
2024-12-22 22:56:58 +08:00
parent c089df66cf
commit 265c669eda
28 changed files with 240 additions and 343 deletions

View File

@@ -10,7 +10,7 @@
</parent>
<artifactId>continew-starter-log-interceptor</artifactId>
<description>ContiNew Starter 日志模块 - 拦截器Spring Boot Actuator HttpTrace 增强版)</description>
<description>ContiNew Starter 日志模块 - 基于拦截器实现Spring Boot Actuator HttpTrace 增强版)</description>
<dependencies>
<!-- Swagger 注解 -->

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.log.interceptor.autoconfigure;
package top.continew.starter.log.autoconfigure;
import jakarta.annotation.PostConstruct;
import org.slf4j.Logger;
@@ -26,11 +26,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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;
import top.continew.starter.log.dao.LogDao;
import top.continew.starter.log.dao.impl.DefaultLogDaoImpl;
import top.continew.starter.log.annotation.ConditionalOnEnabledLog;
import top.continew.starter.log.handler.LogFilter;
import top.continew.starter.log.handler.LogInterceptor;
/**
* 日志自动配置
@@ -71,11 +71,11 @@ public class LogAutoConfiguration implements WebMvcConfigurer {
@Bean
@ConditionalOnMissingBean
public LogDao logDao() {
return new LogDaoDefaultImpl();
return new DefaultLogDaoImpl();
}
@PostConstruct
public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'Log-interceptor' completed initialization.");
log.debug("[ContiNew Starter] - Auto Configuration 'Log-Interceptor' completed initialization.");
}
}

View File

@@ -14,11 +14,11 @@
* limitations under the License.
*/
package top.continew.starter.log.interceptor.autoconfigure;
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.core.enums.Include;
import top.continew.starter.log.enums.Include;
import top.continew.starter.web.util.SpringWebUtils;
import java.util.ArrayList;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.log.interceptor.handler;
package top.continew.starter.log.handler;
import cn.hutool.extra.spring.SpringUtil;
import jakarta.servlet.FilterChain;
@@ -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.interceptor.autoconfigure.LogProperties;
import top.continew.starter.log.autoconfigure.LogProperties;
import java.io.IOException;
import java.net.URI;

View File

@@ -14,7 +14,7 @@
* limitations under the License.
*/
package top.continew.starter.log.interceptor.handler;
package top.continew.starter.log.handler;
import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
@@ -28,13 +28,13 @@ 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.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;
import top.continew.starter.log.interceptor.autoconfigure.LogProperties;
import top.continew.starter.log.http.recordable.impl.RecordableServletHttpRequest;
import top.continew.starter.log.http.recordable.impl.RecordableServletHttpResponse;
import top.continew.starter.log.annotation.Log;
import top.continew.starter.log.dao.LogDao;
import top.continew.starter.log.enums.Include;
import top.continew.starter.log.model.LogRecord;
import top.continew.starter.log.autoconfigure.LogProperties;
import java.time.Duration;
import java.time.Instant;
@@ -109,6 +109,7 @@ public class LogInterceptor implements HandlerInterceptor {
logDao.add(finishedLogRecord);
} catch (Exception ex) {
log.error("Logging http log occurred an error: {}.", ex.getMessage(), ex);
throw ex;
} finally {
timeTtl.remove();
logTtl.remove();

View File

@@ -1,35 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 {
}

View File

@@ -1,65 +0,0 @@
/*
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
* <p>
* 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
* <p>
* http://www.gnu.org/licenses/lgpl.html
* <p>
* 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 top.continew.starter.log.core.enums.Include;
import java.lang.annotation.*;
/**
* 日志注解
* <p>用于接口方法或类上,辅助 Spring Doc 使用效果最佳</p>
*
* @author Charles7c
* @since 1.1.0
*/
@Documented
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Log {
/**
* 日志描述(仅用于接口方法上)
* <p>
* 优先级:@Log("描述") > @Operation(summary="描述")
* </p>
*/
String value() default "";
/**
* 所属模块(用于接口方法或类上)
* <p>
* 优先级: 接口方法上的 @Log(module = "模块") > 接口类上的 @Log(module = "模块") > @Tag(name = "模块") 内容
* </p>
*/
String module() default "";
/**
* 包含信息(在全局配置基础上扩展包含信息)
*/
Include[] includes() default {};
/**
* 排除信息(在全局配置基础上减少包含信息)
*/
Include[] excludes() default {};
/**
* 是否忽略日志记录(用于接口方法或类上)
*/
boolean ignore() default false;
}

View File

@@ -1 +1 @@
top.continew.starter.log.interceptor.autoconfigure.LogAutoConfiguration
top.continew.starter.log.autoconfigure.LogAutoConfiguration