Skip to content

Commit 49bd289

Browse files
committed
refactor(log): 抽取 isRecord 方法方便复用,移除已过期的 timeTtl
1 parent ef6621b commit 49bd289

File tree

5 files changed

+41
-44
lines changed

5 files changed

+41
-44
lines changed

continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aspect/LogAspect.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package top.continew.starter.log.aspect;
1818

19-
import cn.hutool.core.annotation.AnnotationUtil;
2019
import cn.hutool.core.text.CharSequenceUtil;
2120
import org.aspectj.lang.JoinPoint;
2221
import org.aspectj.lang.ProceedingJoinPoint;
@@ -78,7 +77,7 @@ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
7877
// 指定规则不记录
7978
Method targetMethod = this.getMethod(joinPoint);
8079
Class<?> targetClass = joinPoint.getTarget().getClass();
81-
if (!isRequestRecord(targetMethod, targetClass)) {
80+
if (!isRecord(targetMethod, targetClass)) {
8281
return joinPoint.proceed();
8382
}
8483
String errorMsg = null;
@@ -108,13 +107,13 @@ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
108107
}
109108

110109
/**
111-
* 是否要记录日志
110+
* 是否记录日志
112111
*
113112
* @param targetMethod 目标方法
114113
* @param targetClass 目标类
115114
* @return true:需要记录;false:不需要记录
116115
*/
117-
private boolean isRequestRecord(Method targetMethod, Class<?> targetClass) {
116+
private boolean isRecord(Method targetMethod, Class<?> targetClass) {
118117
// 非 Web 环境不记录
119118
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
120119
if (attributes == null || attributes.getResponse() == null) {
@@ -124,13 +123,7 @@ private boolean isRequestRecord(Method targetMethod, Class<?> targetClass) {
124123
if (logProperties.isMatch(attributes.getRequest().getRequestURI())) {
125124
return false;
126125
}
127-
// 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志
128-
Log methodLog = AnnotationUtil.getAnnotation(targetMethod, Log.class);
129-
if (null != methodLog && methodLog.ignore()) {
130-
return false;
131-
}
132-
Log classLog = AnnotationUtil.getAnnotation(targetClass, Log.class);
133-
return null == classLog || !classLog.ignore();
126+
return logHandler.isRecord(targetMethod, targetClass);
134127
}
135128

136129
/**

continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/filter/LogFilter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@
3636
/**
3737
* 日志过滤器
3838
*
39-
* @author Dave Syer(Spring Boot Actuator)
40-
* @author Wallace Wadge(Spring Boot Actuator)
41-
* @author Andy Wilkinson(Spring Boot Actuator)
42-
* @author Venil Noronha(Spring Boot Actuator)
43-
* @author Madhura Bhave(Spring Boot Actuator)
4439
* @author Charles7c
4540
* @author echo
4641
* @since 1.1.0

continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/AbstractLogHandler.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import cn.hutool.core.text.CharSequenceUtil;
2121
import cn.hutool.core.util.ObjectUtil;
2222
import com.alibaba.ttl.TransmittableThreadLocal;
23+
import io.swagger.v3.oas.annotations.Hidden;
2324
import io.swagger.v3.oas.annotations.Operation;
2425
import io.swagger.v3.oas.annotations.tags.Tag;
2526
import org.slf4j.Logger;
@@ -49,6 +50,29 @@ public abstract class AbstractLogHandler implements LogHandler {
4950
private static final Logger log = LoggerFactory.getLogger(AbstractLogHandler.class);
5051
private final TransmittableThreadLocal<AccessLogContext> logContextThread = new TransmittableThreadLocal<>();
5152

53+
@Override
54+
public boolean isRecord(Method targetMethod, Class<?> targetClass) {
55+
// 如果接口被隐藏,不记录日志
56+
Operation methodOperation = AnnotationUtil.getAnnotation(targetMethod, Operation.class);
57+
if (null != methodOperation && methodOperation.hidden()) {
58+
return false;
59+
}
60+
Hidden methodHidden = AnnotationUtil.getAnnotation(targetMethod, Hidden.class);
61+
if (null != methodHidden) {
62+
return false;
63+
}
64+
if (null != targetClass.getDeclaredAnnotation(Hidden.class)) {
65+
return false;
66+
}
67+
// 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志
68+
Log methodLog = AnnotationUtil.getAnnotation(targetMethod, Log.class);
69+
if (null != methodLog && methodLog.ignore()) {
70+
return false;
71+
}
72+
Log classLog = AnnotationUtil.getAnnotation(targetClass, Log.class);
73+
return null == classLog || !classLog.ignore();
74+
}
75+
5276
@Override
5377
public LogRecord.Started start(Instant startTime) {
5478
return LogRecord.start(startTime);

continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/handler/LogHandler.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@
3333
*/
3434
public interface LogHandler {
3535

36+
/**
37+
* 是否记录日志
38+
*
39+
* @param targetMethod 目标方法
40+
* @param targetClass 目标类
41+
* @return 是否记录日志
42+
*/
43+
boolean isRecord(Method targetMethod, Class<?> targetClass);
44+
3645
/**
3746
* 开始日志记录
3847
*

continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,13 @@
1717
package top.continew.starter.log.interceptor;
1818

1919
import com.alibaba.ttl.TransmittableThreadLocal;
20-
import io.swagger.v3.oas.annotations.Hidden;
21-
import io.swagger.v3.oas.annotations.Operation;
2220
import jakarta.servlet.http.HttpServletRequest;
2321
import jakarta.servlet.http.HttpServletResponse;
2422
import org.slf4j.Logger;
2523
import org.slf4j.LoggerFactory;
2624
import org.springframework.lang.NonNull;
2725
import org.springframework.web.method.HandlerMethod;
2826
import org.springframework.web.servlet.HandlerInterceptor;
29-
import top.continew.starter.log.annotation.Log;
3027
import top.continew.starter.log.dao.LogDao;
3128
import top.continew.starter.log.handler.LogHandler;
3229
import top.continew.starter.log.model.AccessLogContext;
@@ -48,7 +45,6 @@ public class LogInterceptor implements HandlerInterceptor {
4845
private final LogProperties logProperties;
4946
private final LogHandler logHandler;
5047
private final LogDao logDao;
51-
private final TransmittableThreadLocal<Instant> timeTtl = new TransmittableThreadLocal<>();
5248
private final TransmittableThreadLocal<LogRecord.Started> logTtl = new TransmittableThreadLocal<>();
5349

5450
public LogInterceptor(LogProperties logProperties, LogHandler logHandler, LogDao logDao) {
@@ -64,7 +60,7 @@ public boolean preHandle(@NonNull HttpServletRequest request,
6460
Instant startTime = Instant.now();
6561
logHandler.accessLogStart(AccessLogContext.builder().startTime(startTime).properties(logProperties).build());
6662
// 开始日志记录
67-
if (this.isRequestRecord(handler, request)) {
63+
if (this.isRecord(handler)) {
6864
LogRecord.Started startedLogRecord = logHandler.start(startTime);
6965
logTtl.set(startedLogRecord);
7066
}
@@ -94,40 +90,20 @@ public void afterCompletion(@NonNull HttpServletRequest request,
9490
log.error("Logging http log occurred an error: {}.", ex.getMessage(), ex);
9591
throw ex;
9692
} finally {
97-
timeTtl.remove();
9893
logTtl.remove();
9994
}
10095
}
10196

10297
/**
103-
* 是否要记录日志
98+
* 是否记录日志
10499
*
105100
* @param handler 处理器
106101
* @return true:需要记录;false:不需要记录
107102
*/
108-
private boolean isRequestRecord(Object handler, HttpServletRequest request) {
103+
private boolean isRecord(Object handler) {
109104
if (!(handler instanceof HandlerMethod handlerMethod)) {
110105
return false;
111106
}
112-
// 如果接口被隐藏,不记录日志
113-
Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class);
114-
if (null != methodOperation && methodOperation.hidden()) {
115-
return false;
116-
}
117-
Hidden methodHidden = handlerMethod.getMethodAnnotation(Hidden.class);
118-
if (null != methodHidden) {
119-
return false;
120-
}
121-
Class<?> handlerBeanType = handlerMethod.getBeanType();
122-
if (null != handlerBeanType.getDeclaredAnnotation(Hidden.class)) {
123-
return false;
124-
}
125-
// 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志
126-
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
127-
if (null != methodLog && methodLog.ignore()) {
128-
return false;
129-
}
130-
Log classLog = handlerBeanType.getDeclaredAnnotation(Log.class);
131-
return null == classLog || !classLog.ignore();
107+
return logHandler.isRecord(handlerMethod.getMethod(), handlerMethod.getBeanType());
132108
}
133109
}

0 commit comments

Comments
 (0)