Skip to content

Commit 265c669

Browse files
committed
refactor(log): 优化基于 AOP 实现日志代码,调整日志整体包结构
1 parent c089df6 commit 265c669

File tree

28 files changed

+240
-343
lines changed

28 files changed

+240
-343
lines changed

continew-starter-dependencies/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,14 +483,14 @@
483483
<version>${revision}</version>
484484
</dependency>
485485

486-
<!-- 日志模块 - 拦截器版(Spring Boot Actuator HttpTrace 增强版) -->
486+
<!-- 日志模块 - 基于拦截器实现(Spring Boot Actuator HttpTrace 增强版) -->
487487
<dependency>
488488
<groupId>top.continew</groupId>
489489
<artifactId>continew-starter-log-interceptor</artifactId>
490490
<version>${revision}</version>
491491
</dependency>
492492

493-
<!-- 日志模块 - 基于 AOP 实现日志记录 -->
493+
<!-- 日志模块 - 基于 AOP 实现 -->
494494
<dependency>
495495
<groupId>top.continew</groupId>
496496
<artifactId>continew-starter-log-aop</artifactId>

continew-starter-log/continew-starter-log-aop/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
<artifactId>continew-starter-log</artifactId>
88
<version>${revision}</version>
99
</parent>
10+
1011
<artifactId>continew-starter-log-aop</artifactId>
11-
<description>ContiNew Starter 日志模块 - 基于 AOP 实现日志记录</description>
12+
<description>ContiNew Starter 日志模块 - 基于 AOP 实现</description>
1213

1314

1415
<dependencies>

continew-starter-log/continew-starter-log-aop/src/main/java/top/continew/starter/log/aop/annotation/Log.java

Lines changed: 0 additions & 54 deletions
This file was deleted.

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

Lines changed: 0 additions & 96 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
3+
* <p>
4+
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* http://www.gnu.org/licenses/lgpl.html
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package top.continew.starter.log.aspect;
18+
19+
import jakarta.servlet.http.HttpServletRequest;
20+
import jakarta.servlet.http.HttpServletResponse;
21+
import org.aspectj.lang.ProceedingJoinPoint;
22+
import org.aspectj.lang.annotation.*;
23+
import org.slf4j.Logger;
24+
import org.slf4j.LoggerFactory;
25+
import org.springframework.web.context.request.RequestContextHolder;
26+
import org.springframework.web.context.request.ServletRequestAttributes;
27+
import top.continew.starter.log.autoconfigure.LogProperties;
28+
29+
import java.time.Duration;
30+
import java.time.Instant;
31+
32+
/**
33+
* 访问日志切面
34+
*
35+
* @author echo
36+
* @author Charles7c
37+
* @since 2.8.0
38+
*/
39+
@Aspect
40+
public class AccessLogAspect {
41+
42+
private static final Logger log = LoggerFactory.getLogger(AccessLogAspect.class);
43+
private final LogProperties logProperties;
44+
45+
public AccessLogAspect(LogProperties logProperties) {
46+
this.logProperties = logProperties;
47+
}
48+
49+
/**
50+
* 切点 - 匹配所有控制器层的方法
51+
*/
52+
@Pointcut("execution(* *..controller.*.*(..)) || execution(* *..*Controller.*(..))")
53+
public void pointcut() {
54+
}
55+
56+
/**
57+
* 打印访问日志
58+
*
59+
* @param joinPoint 切点
60+
* @return 返回结果
61+
* @throws Throwable 异常
62+
*/
63+
@Around("pointcut()")
64+
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
65+
Instant startTime = Instant.now();
66+
// 非 Web 环境不记录
67+
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
68+
if (attributes == null) {
69+
return joinPoint.proceed();
70+
}
71+
HttpServletRequest request = attributes.getRequest();
72+
HttpServletResponse response = attributes.getResponse();
73+
try {
74+
// 打印请求日志
75+
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
76+
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
77+
}
78+
return joinPoint.proceed();
79+
} finally {
80+
Instant endTime = Instant.now();
81+
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
82+
Duration timeTaken = Duration.between(startTime, endTime);
83+
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(), response != null
84+
? response.getStatus()
85+
: "N/A", timeTaken.toMillis());
86+
}
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)