refactor(log/core): 重构请求和响应信息获取

web 模块 ServletUtils整理
log/core 模块 删除 RecordableHttpRequest和
RecordableHttpResponse
This commit is contained in:
吴泽威
2025-04-01 15:58:02 +08:00
parent a6a44cd461
commit ca2c88651f
15 changed files with 264 additions and 460 deletions

View File

@@ -27,12 +27,10 @@ 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.handler.LogHandler;
import top.continew.starter.log.model.AccessLogContext;
import top.continew.starter.log.model.LogProperties;
import top.continew.starter.log.model.LogRecord;
import java.lang.reflect.Method;
@@ -64,14 +62,10 @@ public class LogInterceptor implements HandlerInterceptor {
@NonNull HttpServletResponse response,
@NonNull Object handler) {
Instant startTime = Instant.now();
logHandler.accessLogStart(AccessLogContext.builder()
.startTime(startTime)
.request(new RecordableServletHttpRequest(request))
.properties(logProperties)
.build());
logHandler.accessLogStart(AccessLogContext.builder().startTime(startTime).properties(logProperties).build());
// 开始日志记录
if (this.isRequestRecord(handler, request)) {
LogRecord.Started startedLogRecord = logHandler.start(startTime, request);
LogRecord.Started startedLogRecord = logHandler.start(startTime);
logTtl.set(startedLogRecord);
}
return true;
@@ -84,10 +78,7 @@ public class LogInterceptor implements HandlerInterceptor {
Exception e) {
try {
Instant endTime = Instant.now();
logHandler.accessLogFinish(AccessLogContext.builder()
.endTime(endTime)
.response(new RecordableServletHttpResponse(response, response.getStatus()))
.build());
logHandler.accessLogFinish(AccessLogContext.builder().endTime(endTime).build());
LogRecord.Started startedLogRecord = logTtl.get();
if (null == startedLogRecord) {
return;
@@ -96,7 +87,7 @@ public class LogInterceptor implements HandlerInterceptor {
HandlerMethod handlerMethod = (HandlerMethod)handler;
Method targetMethod = handlerMethod.getMethod();
Class<?> targetClass = handlerMethod.getBeanType();
LogRecord logRecord = logHandler.finish(startedLogRecord, endTime, response, logProperties
LogRecord logRecord = logHandler.finish(startedLogRecord, endTime, logProperties
.getIncludes(), targetMethod, targetClass);
logDao.add(logRecord);
} catch (Exception ex) {