mirror of
https://github.com/continew-org/continew-starter.git
synced 2025-09-08 16:57:09 +08:00
refactor(log): 抽取 isRecord 方法方便复用,移除已过期的 timeTtl
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -36,11 +36,6 @@ import java.net.URISyntaxException;
|
||||
/**
|
||||
* 日志过滤器
|
||||
*
|
||||
* @author Dave Syer(Spring Boot Actuator)
|
||||
* @author Wallace Wadge(Spring Boot Actuator)
|
||||
* @author Andy Wilkinson(Spring Boot Actuator)
|
||||
* @author Venil Noronha(Spring Boot Actuator)
|
||||
* @author Madhura Bhave(Spring Boot Actuator)
|
||||
* @author Charles7c
|
||||
* @author echo
|
||||
* @since 1.1.0
|
||||
|
@@ -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);
|
||||
|
@@ -33,6 +33,15 @@ import java.util.Set;
|
||||
*/
|
||||
public interface LogHandler {
|
||||
|
||||
/**
|
||||
* 是否记录日志
|
||||
*
|
||||
* @param targetMethod 目标方法
|
||||
* @param targetClass 目标类
|
||||
* @return 是否记录日志
|
||||
*/
|
||||
boolean isRecord(Method targetMethod, Class<?> targetClass);
|
||||
|
||||
/**
|
||||
* 开始日志记录
|
||||
*
|
||||
|
@@ -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());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user