refactor(log): 抽取 isRecord 方法方便复用,移除已过期的 timeTtl

This commit is contained in:
2025-05-17 12:09:12 +08:00
parent ef6621bf92
commit 49bd289e29
5 changed files with 41 additions and 44 deletions

View File

@@ -16,7 +16,6 @@
package top.continew.starter.log.aspect;
import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.text.CharSequenceUtil;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
@@ -78,7 +77,7 @@ public class LogAspect {
// 指定规则不记录
Method targetMethod = this.getMethod(joinPoint);
Class<?> targetClass = joinPoint.getTarget().getClass();
if (!isRequestRecord(targetMethod, targetClass)) {
if (!isRecord(targetMethod, targetClass)) {
return joinPoint.proceed();
}
String errorMsg = null;
@@ -108,13 +107,13 @@ public class LogAspect {
}
/**
* 是否记录日志
* 是否记录日志
*
* @param targetMethod 目标方法
* @param targetClass 目标类
* @return true需要记录false不需要记录
*/
private boolean isRequestRecord(Method targetMethod, Class<?> targetClass) {
private boolean isRecord(Method targetMethod, Class<?> targetClass) {
// 非 Web 环境不记录
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
if (attributes == null || attributes.getResponse() == null) {
@@ -124,13 +123,7 @@ public class LogAspect {
if (logProperties.isMatch(attributes.getRequest().getRequestURI())) {
return false;
}
// 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志
Log methodLog = AnnotationUtil.getAnnotation(targetMethod, Log.class);
if (null != methodLog && methodLog.ignore()) {
return false;
}
Log classLog = AnnotationUtil.getAnnotation(targetClass, Log.class);
return null == classLog || !classLog.ignore();
return logHandler.isRecord(targetMethod, targetClass);
}
/**

View File

@@ -36,11 +36,6 @@ import java.net.URISyntaxException;
/**
* 日志过滤器
*
* @author Dave SyerSpring Boot Actuator
* @author Wallace WadgeSpring Boot Actuator
* @author Andy WilkinsonSpring Boot Actuator
* @author Venil NoronhaSpring Boot Actuator
* @author Madhura BhaveSpring Boot Actuator
* @author Charles7c
* @author echo
* @since 1.1.0

View File

@@ -20,6 +20,7 @@ import cn.hutool.core.annotation.AnnotationUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.ttl.TransmittableThreadLocal;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
@@ -49,6 +50,29 @@ public abstract class AbstractLogHandler implements LogHandler {
private static final Logger log = LoggerFactory.getLogger(AbstractLogHandler.class);
private final TransmittableThreadLocal<AccessLogContext> logContextThread = new TransmittableThreadLocal<>();
@Override
public boolean isRecord(Method targetMethod, Class<?> targetClass) {
// 如果接口被隐藏,不记录日志
Operation methodOperation = AnnotationUtil.getAnnotation(targetMethod, Operation.class);
if (null != methodOperation && methodOperation.hidden()) {
return false;
}
Hidden methodHidden = AnnotationUtil.getAnnotation(targetMethod, Hidden.class);
if (null != methodHidden) {
return false;
}
if (null != targetClass.getDeclaredAnnotation(Hidden.class)) {
return false;
}
// 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志
Log methodLog = AnnotationUtil.getAnnotation(targetMethod, Log.class);
if (null != methodLog && methodLog.ignore()) {
return false;
}
Log classLog = AnnotationUtil.getAnnotation(targetClass, Log.class);
return null == classLog || !classLog.ignore();
}
@Override
public LogRecord.Started start(Instant startTime) {
return LogRecord.start(startTime);

View File

@@ -33,6 +33,15 @@ import java.util.Set;
*/
public interface LogHandler {
/**
* 是否记录日志
*
* @param targetMethod 目标方法
* @param targetClass 目标类
* @return 是否记录日志
*/
boolean isRecord(Method targetMethod, Class<?> targetClass);
/**
* 开始日志记录
*

View File

@@ -17,8 +17,6 @@
package top.continew.starter.log.interceptor;
import com.alibaba.ttl.TransmittableThreadLocal;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.slf4j.Logger;
@@ -26,7 +24,6 @@ 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.annotation.Log;
import top.continew.starter.log.dao.LogDao;
import top.continew.starter.log.handler.LogHandler;
import top.continew.starter.log.model.AccessLogContext;
@@ -48,7 +45,6 @@ public class LogInterceptor implements HandlerInterceptor {
private final LogProperties logProperties;
private final LogHandler logHandler;
private final LogDao logDao;
private final TransmittableThreadLocal<Instant> timeTtl = new TransmittableThreadLocal<>();
private final TransmittableThreadLocal<LogRecord.Started> logTtl = new TransmittableThreadLocal<>();
public LogInterceptor(LogProperties logProperties, LogHandler logHandler, LogDao logDao) {
@@ -64,7 +60,7 @@ public class LogInterceptor implements HandlerInterceptor {
Instant startTime = Instant.now();
logHandler.accessLogStart(AccessLogContext.builder().startTime(startTime).properties(logProperties).build());
// 开始日志记录
if (this.isRequestRecord(handler, request)) {
if (this.isRecord(handler)) {
LogRecord.Started startedLogRecord = logHandler.start(startTime);
logTtl.set(startedLogRecord);
}
@@ -94,40 +90,20 @@ public class LogInterceptor implements HandlerInterceptor {
log.error("Logging http log occurred an error: {}.", ex.getMessage(), ex);
throw ex;
} finally {
timeTtl.remove();
logTtl.remove();
}
}
/**
* 是否记录日志
* 是否记录日志
*
* @param handler 处理器
* @return true需要记录false不需要记录
*/
private boolean isRequestRecord(Object handler, HttpServletRequest request) {
private boolean isRecord(Object handler) {
if (!(handler instanceof HandlerMethod handlerMethod)) {
return false;
}
// 如果接口被隐藏,不记录日志
Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class);
if (null != methodOperation && methodOperation.hidden()) {
return false;
}
Hidden methodHidden = handlerMethod.getMethodAnnotation(Hidden.class);
if (null != methodHidden) {
return false;
}
Class<?> handlerBeanType = handlerMethod.getBeanType();
if (null != handlerBeanType.getDeclaredAnnotation(Hidden.class)) {
return false;
}
// 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
if (null != methodLog && methodLog.ignore()) {
return false;
}
Log classLog = handlerBeanType.getDeclaredAnnotation(Log.class);
return null == classLog || !classLog.ignore();
return logHandler.isRecord(handlerMethod.getMethod(), handlerMethod.getBeanType());
}
}