Skip to content

Commit c5cb203

Browse files
committed
refactor(log): 优化日志模块
1 parent 5ff9391 commit c5cb203

File tree

8 files changed

+57
-94
lines changed

8 files changed

+57
-94
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import org.slf4j.LoggerFactory;
2727
import org.springframework.web.context.request.RequestContextHolder;
2828
import org.springframework.web.context.request.ServletRequestAttributes;
29-
import top.continew.starter.log.autoconfigure.LogProperties;
29+
import top.continew.starter.log.model.LogProperties;
3030

3131
import java.time.Duration;
3232
import java.time.Instant;
@@ -51,42 +51,42 @@ public AccessLogAspect(LogProperties logProperties) {
5151
/**
5252
* 切点 - 匹配所有控制器层的 GET 请求方法
5353
*/
54-
@Pointcut("within(@org.springframework.web.bind.annotation.RequestMapping *)")
54+
@Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
5555
public void pointcut() {
5656
}
5757

5858
/**
5959
* 切点 - 匹配所有控制器层的 GET 请求方法
6060
*/
61-
@Pointcut("within(@org.springframework.web.bind.annotation.GetMapping *)")
61+
@Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
6262
public void pointcutGet() {
6363
}
6464

6565
/**
6666
* 切点 - 匹配所有控制器层的 POST 请求方法
6767
*/
68-
@Pointcut("within(@org.springframework.web.bind.annotation.PostMapping *)")
68+
@Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
6969
public void pointcutPost() {
7070
}
7171

7272
/**
7373
* 切点 - 匹配所有控制器层的 PUT 请求方法
7474
*/
75-
@Pointcut("within(@org.springframework.web.bind.annotation.PutMapping *)")
75+
@Pointcut("@annotation(org.springframework.web.bind.annotation.PutMapping)")
7676
public void pointcutPut() {
7777
}
7878

7979
/**
8080
* 切点 - 匹配所有控制器层的 DELETE 请求方法
8181
*/
82-
@Pointcut("within(@org.springframework.web.bind.annotation.DeleteMapping *)")
82+
@Pointcut("@annotation(org.springframework.web.bind.annotation.DeleteMapping)")
8383
public void pointcutDelete() {
8484
}
8585

8686
/**
8787
* 切点 - 匹配所有控制器层的 PATCH 请求方法
8888
*/
89-
@Pointcut("within(@org.springframework.web.bind.annotation.PatchMapping *)")
89+
@Pointcut("@annotation(org.springframework.web.bind.annotation.PatchMapping)")
9090
public void pointcutPatch() {
9191
}
9292

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

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

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

19+
import cn.hutool.core.annotation.AnnotationUtil;
1920
import cn.hutool.core.text.CharSequenceUtil;
2021
import jakarta.servlet.http.HttpServletRequest;
2122
import jakarta.servlet.http.HttpServletResponse;
@@ -30,10 +31,11 @@
3031
import org.springframework.web.context.request.RequestContextHolder;
3132
import org.springframework.web.context.request.ServletRequestAttributes;
3233
import top.continew.starter.log.annotation.Log;
33-
import top.continew.starter.log.autoconfigure.LogProperties;
3434
import top.continew.starter.log.dao.LogDao;
3535
import top.continew.starter.log.handler.LogHandler;
36+
import top.continew.starter.log.model.LogProperties;
3637
import top.continew.starter.log.model.LogRecord;
38+
import top.continew.starter.web.util.SpringWebUtils;
3739

3840
import java.lang.reflect.Method;
3941
import java.time.Instant;
@@ -76,13 +78,13 @@ public void pointcut() {
7678
@Around("pointcut()")
7779
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
7880
Instant startTime = Instant.now();
79-
// 非 Web 环境不记录
80-
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
81-
if (attributes == null || attributes.getResponse() == null) {
81+
// 指定规则不记录
82+
HttpServletRequest request = SpringWebUtils.getRequest();
83+
Method targetMethod = this.getMethod(joinPoint);
84+
Class<?> targetClass = joinPoint.getTarget().getClass();
85+
if (!isRequestRecord(targetMethod, targetClass)) {
8286
return joinPoint.proceed();
8387
}
84-
HttpServletRequest request = attributes.getRequest();
85-
HttpServletResponse response = attributes.getResponse();
8688
String errorMsg = null;
8789
// 开始记录
8890
LogRecord.Started startedLogRecord = logHandler.start(startTime, request);
@@ -95,8 +97,7 @@ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
9597
} finally {
9698
try {
9799
Instant endTime = Instant.now();
98-
Method targetMethod = this.getMethod(joinPoint);
99-
Class<?> targetClass = joinPoint.getTarget().getClass();
100+
HttpServletResponse response = SpringWebUtils.getResponse();
100101
LogRecord logRecord = logHandler.finish(startedLogRecord, endTime, response, logProperties
101102
.getIncludes(), targetMethod, targetClass);
102103
// 记录异常信息
@@ -111,6 +112,32 @@ public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
111112
}
112113
}
113114

115+
/**
116+
* 是否要记录日志
117+
*
118+
* @param targetMethod 目标方法
119+
* @param targetClass 目标类
120+
* @return true:需要记录;false:不需要记录
121+
*/
122+
private boolean isRequestRecord(Method targetMethod, Class<?> targetClass) {
123+
// 非 Web 环境不记录
124+
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
125+
if (attributes == null || attributes.getResponse() == null) {
126+
return false;
127+
}
128+
// 如果接口匹配排除列表,不记录日志
129+
if (logProperties.isMatch(attributes.getRequest().getRequestURI())) {
130+
return false;
131+
}
132+
// 如果接口方法或类上有 @Log 注解,且要求忽略该接口,则不记录日志
133+
Log methodLog = AnnotationUtil.getAnnotation(targetMethod, Log.class);
134+
if (null != methodLog && methodLog.ignore()) {
135+
return false;
136+
}
137+
Log classLog = AnnotationUtil.getAnnotation(targetClass, Log.class);
138+
return null == classLog || !classLog.ignore();
139+
}
140+
114141
/**
115142
* 获取方法
116143
*

continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@
3030
import top.continew.starter.log.dao.LogDao;
3131
import top.continew.starter.log.dao.impl.DefaultLogDaoImpl;
3232
import top.continew.starter.log.handler.AopLogHandler;
33+
import top.continew.starter.log.handler.LogFilter;
3334
import top.continew.starter.log.handler.LogHandler;
35+
import top.continew.starter.log.model.LogProperties;
3436

3537
/**
3638
* 日志自动配置
@@ -52,6 +54,15 @@ public LogAutoConfiguration(LogProperties logProperties) {
5254
this.logProperties = logProperties;
5355
}
5456

57+
/**
58+
* 日志过滤器
59+
*/
60+
@Bean
61+
@ConditionalOnMissingBean
62+
public LogFilter logFilter() {
63+
return new LogFilter(logProperties);
64+
}
65+
5566
/**
5667
* 日志切面
5768
*

continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/autoconfigure/LogProperties.java

Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import org.springframework.web.util.ContentCachingRequestWrapper;
2929
import org.springframework.web.util.ContentCachingResponseWrapper;
3030
import org.springframework.web.util.WebUtils;
31-
import top.continew.starter.log.autoconfigure.LogProperties;
31+
import top.continew.starter.log.model.LogProperties;
3232

3333
import java.io.IOException;
3434
import java.net.URI;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package top.continew.starter.log.autoconfigure;
17+
package top.continew.starter.log.model;
1818

1919
import org.springframework.boot.context.properties.ConfigurationProperties;
2020
import top.continew.starter.core.constant.PropertiesConstants;

continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/autoconfigure/LogAutoConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import top.continew.starter.log.handler.LogFilter;
3434
import top.continew.starter.log.handler.LogHandler;
3535
import top.continew.starter.log.interceptor.LogInterceptor;
36+
import top.continew.starter.log.model.LogProperties;
3637

3738
/**
3839
* 日志自动配置

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.springframework.web.method.HandlerMethod;
2828
import org.springframework.web.servlet.HandlerInterceptor;
2929
import top.continew.starter.log.annotation.Log;
30-
import top.continew.starter.log.autoconfigure.LogProperties;
30+
import top.continew.starter.log.model.LogProperties;
3131
import top.continew.starter.log.dao.LogDao;
3232
import top.continew.starter.log.handler.LogHandler;
3333
import top.continew.starter.log.model.LogRecord;

0 commit comments

Comments
 (0)