Skip to content

Commit 7c3f15a

Browse files
Time-wCharles7c
authored andcommitted
feat(log/aop): 新增 log-aop 组件模块(基于 AOP 实现日志记录)
1 parent 7587417 commit 7c3f15a

File tree

21 files changed

+614
-11
lines changed

21 files changed

+614
-11
lines changed

continew-starter-dependencies/pom.xml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,13 @@
490490
<version>${revision}</version>
491491
</dependency>
492492

493+
<!-- 日志模块 - 基于 AOP 实现日志记录 -->
494+
<dependency>
495+
<groupId>top.continew</groupId>
496+
<artifactId>continew-starter-log-aop</artifactId>
497+
<version>${revision}</version>
498+
</dependency>
499+
493500
<!-- 日志模块 - 核心模块 -->
494501
<dependency>
495502
<groupId>top.continew</groupId>
@@ -680,7 +687,8 @@
680687
<configuration>
681688
<artifacts>
682689
<artifact>
683-
<file>${project.build.directory}/effective-pom/continew-starter-dependencies.xml</file>
690+
<file>${project.build.directory}/effective-pom/continew-starter-dependencies.xml
691+
</file>
684692
<type>effective-pom</type>
685693
</artifact>
686694
</artifacts>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>top.continew</groupId>
7+
<artifactId>continew-starter-log</artifactId>
8+
<version>${revision}</version>
9+
</parent>
10+
<artifactId>continew-starter-log-aop</artifactId>
11+
<description>ContiNew Starter 日志模块 - 基于 AOP 实现日志记录</description>
12+
13+
14+
<dependencies>
15+
<!-- 日志模块 - 核心模块 -->
16+
<dependency>
17+
<groupId>top.continew</groupId>
18+
<artifactId>continew-starter-log-core</artifactId>
19+
</dependency>
20+
</dependencies>
21+
</project>
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.interceptor.autoconfigure;
17+
package top.continew.starter.log.aop.annotation;
1818

1919
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2020
import top.continew.starter.core.constant.PropertiesConstants;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.aop.annotation;
18+
19+
import top.continew.starter.log.core.enums.Include;
20+
21+
import java.lang.annotation.*;
22+
23+
/**
24+
* 日志注解
25+
* <p>用于接口方法或类上</p>
26+
*
27+
* @author Charles7c
28+
* @since 1.1.0
29+
*/
30+
@Documented
31+
@Target({ElementType.METHOD, ElementType.TYPE})
32+
@Retention(RetentionPolicy.RUNTIME)
33+
public @interface Log {
34+
35+
/**
36+
* 所属模块(用于接口方法或类上)
37+
*/
38+
String module() default "";
39+
40+
/**
41+
* 日志描述 - 接口操作内容(仅用于接口方法上)
42+
*/
43+
String value() default "";
44+
45+
/**
46+
* 包含信息(在全局配置基础上扩展包含信息)
47+
*/
48+
Include[] includes() default {};
49+
50+
/**
51+
* 排除信息(在全局配置基础上减少包含信息)
52+
*/
53+
Include[] excludes() default {};
54+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package top.continew.starter.log.aop.aspect;
2+
3+
import com.alibaba.ttl.TransmittableThreadLocal;
4+
import jakarta.servlet.http.HttpServletRequest;
5+
import jakarta.servlet.http.HttpServletResponse;
6+
import org.aspectj.lang.annotation.After;
7+
import org.aspectj.lang.annotation.Aspect;
8+
import org.aspectj.lang.annotation.Before;
9+
import org.aspectj.lang.annotation.Pointcut;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
import org.springframework.web.context.request.RequestContextHolder;
13+
import org.springframework.web.context.request.ServletRequestAttributes;
14+
import top.continew.starter.log.aop.autoconfigure.LogProperties;
15+
16+
import java.time.Duration;
17+
import java.time.Instant;
18+
19+
/**
20+
* 控制台 输出日志切面
21+
*
22+
* @author echo
23+
* @date 2024/12/06 10:33
24+
**/
25+
@Aspect
26+
public class ConsoleLogAspect {
27+
28+
private static final Logger log = LoggerFactory.getLogger(ConsoleLogAspect.class);
29+
private final LogProperties logProperties;
30+
private final TransmittableThreadLocal<Instant> timeTtl = new TransmittableThreadLocal<>();
31+
32+
public ConsoleLogAspect(LogProperties logProperties) {
33+
this.logProperties = logProperties;
34+
}
35+
36+
/**
37+
* 切点 - 匹配所有控制器层的方法
38+
*/
39+
@Pointcut("execution(* *..controller.*.*(..)) || execution(* *..*Controller.*(..))")
40+
public void controllerLayer() {
41+
}
42+
43+
/**
44+
* 处理请求前执行
45+
*/
46+
@Before(value = "controllerLayer()")
47+
public void doBefore() {
48+
// 打印请求日志
49+
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
50+
Instant startTime = Instant.now();
51+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
52+
if (attributes != null) {
53+
HttpServletRequest request = attributes.getRequest();
54+
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
55+
}
56+
timeTtl.set(startTime);
57+
}
58+
}
59+
60+
/**
61+
* 处理请求后执行
62+
*/
63+
@After(value = "controllerLayer()")
64+
public void afterAdvice() {
65+
// 打印请求耗时
66+
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
67+
Instant endTime = Instant.now();
68+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
69+
if (attributes == null) {
70+
return;
71+
}
72+
HttpServletRequest request = attributes.getRequest();
73+
HttpServletResponse response = attributes.getResponse();
74+
Duration timeTaken = Duration.between(timeTtl.get(), endTime);
75+
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(),
76+
response != null ? response.getStatus() : "N/A",
77+
timeTaken.toMillis());
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)