feat(core): 新增请求响应可重复读流处理并优化日志模块

增加访问日志打印处理:包括参数打印、过滤敏感参数和超长参数配置
This commit is contained in:
liquor
2025-03-25 13:09:06 +00:00
committed by Charles7c
parent 1903520433
commit da5e162a2a
20 changed files with 811 additions and 101 deletions

View File

@@ -26,9 +26,12 @@ 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.handler.LogHandler;
import top.continew.starter.log.http.servlet.RecordableServletHttpRequest;
import top.continew.starter.log.http.servlet.RecordableServletHttpResponse;
import top.continew.starter.log.model.AccessLogContext;
import top.continew.starter.log.model.LogProperties;
import java.time.Duration;
import java.time.Instant;
/**
@@ -43,9 +46,11 @@ public class AccessLogAspect {
private static final Logger log = LoggerFactory.getLogger(AccessLogAspect.class);
private final LogProperties logProperties;
private final LogHandler logHandler;
public AccessLogAspect(LogProperties logProperties) {
public AccessLogAspect(LogProperties logProperties, LogHandler logHandler) {
this.logProperties = logProperties;
this.logHandler = logHandler;
}
/**
@@ -108,19 +113,18 @@ public class AccessLogAspect {
HttpServletRequest request = attributes.getRequest();
HttpServletResponse response = attributes.getResponse();
try {
// 打印请求日志
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
}
logHandler.processAccessLogStartReq(AccessLogContext.builder()
.startTime(startTime)
.request(new RecordableServletHttpRequest(request))
.properties(logProperties)
.build());
return joinPoint.proceed();
} finally {
Instant endTime = Instant.now();
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
Duration timeTaken = Duration.between(startTime, endTime);
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), response != null
? response.getStatus()
: "N/A", timeTaken.toMillis());
}
logHandler.processAccessLogEndReq(AccessLogContext.builder()
.endTime(endTime)
.response(new RecordableServletHttpResponse(response, response.getStatus()))
.build());
}
}
}

View File

@@ -32,7 +32,7 @@ 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.dao.LogDao;
import top.continew.starter.log.LogHandler;
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;

View File

@@ -29,9 +29,9 @@ import top.continew.starter.log.aspect.AccessLogAspect;
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.filter.LogFilter;
import top.continew.starter.log.handler.AopLogHandler;
import top.continew.starter.log.LogFilter;
import top.continew.starter.log.LogHandler;
import top.continew.starter.log.handler.LogHandler;
import top.continew.starter.log.model.LogProperties;
/**
@@ -49,9 +49,11 @@ public class LogAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(LogAutoConfiguration.class);
private final LogProperties logProperties;
private final LogHandler logHandler;
public LogAutoConfiguration(LogProperties logProperties) {
public LogAutoConfiguration(LogProperties logProperties, LogHandler logHandler) {
this.logProperties = logProperties;
this.logHandler = logHandler;
}
/**
@@ -66,13 +68,12 @@ public class LogAutoConfiguration {
/**
* 日志切面
*
* @param logHandler 日志处理器
* @param logDao 日志持久层接口
* @param logDao 日志持久层接口
* @return {@link LogAspect }
*/
@Bean
@ConditionalOnMissingBean
public LogAspect logAspect(LogHandler logHandler, LogDao logDao) {
public LogAspect logAspect(LogDao logDao) {
return new LogAspect(logProperties, logHandler, logDao);
}
@@ -84,7 +85,7 @@ public class LogAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public AccessLogAspect accessLogAspect() {
return new AccessLogAspect(logProperties);
return new AccessLogAspect(logProperties, logHandler);
}
/**

View File

@@ -16,8 +16,6 @@
package top.continew.starter.log.handler;
import top.continew.starter.log.AbstractLogHandler;
/**
* 日志处理器-AOP 版实现
*