Skip to content

Commit a5c7e07

Browse files
committed
feat(log/aop): 新增 log-aop 组件模块,基于 aop 记录日志记录
1 parent 06a99f5 commit a5c7e07

File tree

21 files changed

+619
-11
lines changed

21 files changed

+619
-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: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.JoinPoint;
7+
import org.aspectj.lang.annotation.AfterReturning;
8+
import org.aspectj.lang.annotation.Aspect;
9+
import org.aspectj.lang.annotation.Before;
10+
import org.aspectj.lang.annotation.Pointcut;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
import org.springframework.web.context.request.RequestContextHolder;
14+
import org.springframework.web.context.request.ServletRequestAttributes;
15+
import top.continew.starter.log.aop.autoconfigure.LogProperties;
16+
17+
import java.time.Duration;
18+
import java.time.Instant;
19+
20+
/**
21+
* 控制台 输出日志切面
22+
*
23+
* @author echo
24+
* @date 2024/12/06 10:33
25+
**/
26+
@Aspect
27+
public class ConsoleLogAspect {
28+
29+
private static final Logger log = LoggerFactory.getLogger(ConsoleLogAspect.class);
30+
private final LogProperties logProperties;
31+
private final TransmittableThreadLocal<Instant> timeTtl = new TransmittableThreadLocal<>();
32+
33+
public ConsoleLogAspect(LogProperties logProperties) {
34+
this.logProperties = logProperties;
35+
}
36+
37+
/**
38+
* 切点 - 匹配所有控制器层的方法
39+
*/
40+
@Pointcut("execution(* *..controller.*.*(..)) || execution(* *..*Controller.*(..))")
41+
public void controllerLayer() {
42+
}
43+
44+
/**
45+
* 处理请求前执行
46+
*/
47+
@Before(value = "controllerLayer()")
48+
public void doBefore() {
49+
// 打印请求日志
50+
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
51+
Instant startTime = Instant.now();
52+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
53+
if (attributes != null) {
54+
HttpServletRequest request = attributes.getRequest();
55+
log.info("[{}] {}", request.getMethod(), request.getRequestURI());
56+
}
57+
timeTtl.set(startTime);
58+
}
59+
}
60+
61+
/**
62+
* 处理请求后执行 - 正常返回
63+
*
64+
* @param joinPoint 切点
65+
*/
66+
@AfterReturning(pointcut = "controllerLayer()", returning = "joinPoint")
67+
public void afterReturning(JoinPoint joinPoint) {
68+
// 打印请求耗时
69+
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
70+
Instant endTime = Instant.now();
71+
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
72+
if (attributes == null) {
73+
return;
74+
}
75+
HttpServletRequest request = attributes.getRequest();
76+
HttpServletResponse response = attributes.getResponse();
77+
Duration timeTaken = Duration.between(timeTtl.get(), endTime);
78+
log.info("[{}] {} {} {}ms", request.getMethod(), request.getRequestURI(),
79+
response != null ? response.getStatus() : "N/A",
80+
timeTaken.toMillis());
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)