feat:引入切面记录日志
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -12,3 +12,4 @@
|
|||||||
enlish-service/target
|
enlish-service/target
|
||||||
enlish-service/src/test
|
enlish-service/src/test
|
||||||
enlish-framework/enlish-common/target
|
enlish-framework/enlish-common/target
|
||||||
|
enlish-framework/enlish-spring-boot-starter-biz-operationlog/target
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<!-- 指定父项目 -->
|
||||||
|
<parent>
|
||||||
|
<groupId>com.yinlihupo</groupId>
|
||||||
|
<artifactId>enlish-framework</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<!-- 指定打包方式 -->
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<artifactId>enlish-spring-boot-starter-biz-operationlog</artifactId>
|
||||||
|
<name>${project.artifactId}</name>
|
||||||
|
<description>接口日志组件</description>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.yinlihupo</groupId>
|
||||||
|
<artifactId>enlish-common</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- AOP 切面 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-aop</artifactId>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
|
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.yinlihupo.framework.biz.operationlog.aspect;
|
||||||
|
|
||||||
|
import java.lang.annotation.*;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target({ElementType.METHOD})
|
||||||
|
@Documented
|
||||||
|
public @interface ApiOperationLog {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* API 功能描述
|
||||||
|
*/
|
||||||
|
String description() default "";
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
|
||||||
|
package com.yinlihupo.framework.biz.operationlog.aspect;
|
||||||
|
|
||||||
|
|
||||||
|
import com.yinlihupo.framework.common.util.JsonUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.aspectj.lang.reflect.MethodSignature;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.function.Function;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
@Slf4j
|
||||||
|
public class ApiOperationLogAspect {
|
||||||
|
|
||||||
|
/** 以自定义 @ApiOperationLog 注解为切点,凡是添加 @ApiOperationLog 的方法,都会执行环绕中的代码 */
|
||||||
|
@Pointcut("@annotation(com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog)")
|
||||||
|
public void apiOperationLog() {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 环绕
|
||||||
|
*/
|
||||||
|
@Around("apiOperationLog()")
|
||||||
|
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
// 请求开始时间
|
||||||
|
long startTime = System.currentTimeMillis();
|
||||||
|
|
||||||
|
// 获取被请求的类和方法
|
||||||
|
String className = joinPoint.getTarget().getClass().getSimpleName();
|
||||||
|
String methodName = joinPoint.getSignature().getName();
|
||||||
|
|
||||||
|
// 请求入参
|
||||||
|
Object[] args = joinPoint.getArgs();
|
||||||
|
// 入参转 JSON 字符串
|
||||||
|
String argsJsonStr = Arrays.stream(args).map(toJsonStr()).collect(Collectors.joining(", "));
|
||||||
|
|
||||||
|
// 功能描述信息
|
||||||
|
String description = getApiOperationLogDescription(joinPoint);
|
||||||
|
|
||||||
|
// 打印请求相关参数
|
||||||
|
log.info("====== 请求开始: [{}], 入参: {}, 请求类: {}, 请求方法: {} =================================== ",
|
||||||
|
description, argsJsonStr, className, methodName);
|
||||||
|
|
||||||
|
// 执行切点方法
|
||||||
|
Object result = joinPoint.proceed();
|
||||||
|
|
||||||
|
// 执行耗时
|
||||||
|
long executionTime = System.currentTimeMillis() - startTime;
|
||||||
|
|
||||||
|
// 打印出参等相关信息
|
||||||
|
log.info("====== 请求结束: [{}], 耗时: {}ms, 出参: {} =================================== ",
|
||||||
|
description, executionTime, JsonUtils.toJsonString(result));
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取注解的描述信息
|
||||||
|
*/
|
||||||
|
private String getApiOperationLogDescription(ProceedingJoinPoint joinPoint) {
|
||||||
|
// 1. 从 ProceedingJoinPoint 获取 MethodSignature
|
||||||
|
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||||
|
|
||||||
|
// 2. 使用 MethodSignature 获取当前被注解的 Method
|
||||||
|
Method method = signature.getMethod();
|
||||||
|
|
||||||
|
// 3. 从 Method 中提取 LogExecution 注解
|
||||||
|
ApiOperationLog apiOperationLog = method.getAnnotation(ApiOperationLog.class);
|
||||||
|
|
||||||
|
// 4. 从 LogExecution 注解中获取 description 属性
|
||||||
|
return apiOperationLog.description();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 转 JSON 字符串
|
||||||
|
*/
|
||||||
|
private Function<Object, String> toJsonStr() {
|
||||||
|
return JsonUtils::toJsonString;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
package com.yinlihupo.framework.biz.operationlog.config;
|
||||||
|
|
||||||
|
|
||||||
|
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLogAspect;
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
|
||||||
|
|
||||||
|
@AutoConfiguration
|
||||||
|
public class ApiOperationLogAutoConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ApiOperationLogAspect apiOperationLogAspect() {
|
||||||
|
return new ApiOperationLogAspect();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
com.yinlihupo.framework.biz.operationlog.config.ApiOperationLogAutoConfiguration
|
||||||
@@ -20,6 +20,7 @@
|
|||||||
|
|
||||||
<modules>
|
<modules>
|
||||||
<module>enlish-common</module>
|
<module>enlish-common</module>
|
||||||
|
<module>enlish-spring-boot-starter-biz-operationlog</module>
|
||||||
</modules>
|
</modules>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -24,6 +24,12 @@
|
|||||||
<artifactId>enlish-common</artifactId>
|
<artifactId>enlish-common</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 业务接口日志组件 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.yinlihupo</groupId>
|
||||||
|
<artifactId>enlish-spring-boot-starter-biz-operationlog</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-web</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
package com.yinlihupo.enlish.service.deoms.web;
|
package com.yinlihupo.enlish.service.deoms.web;
|
||||||
|
|
||||||
|
import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog;
|
||||||
import com.yinlihupo.framework.common.response.Response;
|
import com.yinlihupo.framework.common.response.Response;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
public class TestController {
|
public class TestController {
|
||||||
|
|
||||||
@GetMapping("/test")
|
@GetMapping("/test")
|
||||||
|
@ApiOperationLog(description = "测试接口")
|
||||||
public Response<String> test() {
|
public Response<String> test() {
|
||||||
return Response.success("Hello, 犬小哈专栏");
|
return Response.success("Hello, 犬小哈专栏");
|
||||||
}
|
}
|
||||||
|
|||||||
7
pom.xml
7
pom.xml
@@ -70,6 +70,13 @@
|
|||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- 业务接口日志组件 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.yinlihupo</groupId>
|
||||||
|
<artifactId>enlish-spring-boot-starter-biz-operationlog</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 避免编写那些冗余的 Java 样板式代码,如 get、set 等 -->
|
<!-- 避免编写那些冗余的 Java 样板式代码,如 get、set 等 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
|
|||||||
Reference in New Issue
Block a user