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

@@ -30,8 +30,8 @@ import top.continew.starter.log.annotation.ConditionalOnEnabledLog;
import top.continew.starter.log.dao.LogDao;
import top.continew.starter.log.dao.impl.DefaultLogDaoImpl;
import top.continew.starter.log.handler.InterceptorLogHandler;
import top.continew.starter.log.LogFilter;
import top.continew.starter.log.LogHandler;
import top.continew.starter.log.filter.LogFilter;
import top.continew.starter.log.handler.LogHandler;
import top.continew.starter.log.interceptor.LogInterceptor;
import top.continew.starter.log.model.LogProperties;

View File

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

View File

@@ -27,13 +27,15 @@ 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.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 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.LogRecord;
import java.lang.reflect.Method;
import java.time.Duration;
import java.time.Instant;
/**
@@ -62,10 +64,11 @@ public class LogInterceptor implements HandlerInterceptor {
@NonNull HttpServletResponse response,
@NonNull Object handler) {
Instant startTime = Instant.now();
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
timeTtl.set(startTime);
}
logHandler.processAccessLogStartReq(AccessLogContext.builder()
.startTime(startTime)
.request(new RecordableServletHttpRequest(request))
.properties(logProperties)
.build());
// 开始日志记录
if (this.isRequestRecord(handler, request)) {
LogRecord.Started startedLogRecord = logHandler.start(startTime, request);
@@ -81,11 +84,10 @@ public class LogInterceptor implements HandlerInterceptor {
Exception e) {
try {
Instant endTime = Instant.now();
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
Duration timeTaken = Duration.between(timeTtl.get(), endTime);
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), response
.getStatus(), timeTaken.toMillis());
}
logHandler.processAccessLogEndReq(AccessLogContext.builder()
.endTime(endTime)
.response(new RecordableServletHttpResponse(response, response.getStatus()))
.build());
LogRecord.Started startedLogRecord = logTtl.get();
if (null == startedLogRecord) {
return;