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

@@ -16,8 +16,6 @@
package top.continew.starter.log.aspect;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
@@ -25,8 +23,6 @@ import org.aspectj.lang.annotation.Pointcut;
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;
@@ -107,22 +103,16 @@ public class AccessLogAspect {
if (attributes == null) {
return joinPoint.proceed();
}
HttpServletRequest request = attributes.getRequest();
HttpServletResponse response = attributes.getResponse();
try {
// 开始访问日志记录
logHandler.accessLogStart(AccessLogContext.builder()
.startTime(startTime)
.request(new RecordableServletHttpRequest(request))
.properties(logProperties)
.build());
return joinPoint.proceed();
} finally {
Instant endTime = Instant.now();
logHandler.accessLogFinish(AccessLogContext.builder()
.endTime(endTime)
.response(new RecordableServletHttpResponse(response, response.getStatus()))
.build());
logHandler.accessLogFinish(AccessLogContext.builder().endTime(endTime).build());
}
}
}

View File

@@ -18,8 +18,6 @@ package top.continew.starter.log.aspect;
import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.text.CharSequenceUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
@@ -35,7 +33,6 @@ import top.continew.starter.log.dao.LogDao;
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;
import java.lang.reflect.Method;
import java.time.Instant;
@@ -79,7 +76,6 @@ public class LogAspect {
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Instant startTime = Instant.now();
// 指定规则不记录
HttpServletRequest request = SpringWebUtils.getRequest();
Method targetMethod = this.getMethod(joinPoint);
Class<?> targetClass = joinPoint.getTarget().getClass();
if (!isRequestRecord(targetMethod, targetClass)) {
@@ -87,7 +83,7 @@ public class LogAspect {
}
String errorMsg = null;
// 开始记录
LogRecord.Started startedLogRecord = logHandler.start(startTime, request);
LogRecord.Started startedLogRecord = logHandler.start(startTime);
try {
// 执行目标方法
return joinPoint.proceed();
@@ -97,8 +93,7 @@ public class LogAspect {
} finally {
try {
Instant endTime = Instant.now();
HttpServletResponse response = SpringWebUtils.getResponse();
LogRecord logRecord = logHandler.finish(startedLogRecord, endTime, response, logProperties
LogRecord logRecord = logHandler.finish(startedLogRecord, endTime, logProperties
.getIncludes(), targetMethod, targetClass);
// 记录异常信息
if (errorMsg != null) {