diff --git a/.gitignore b/.gitignore index 86cd45e..f657956 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,5 @@ .idea enlish-service/target enlish-service/src/test -enlish-framework/enlish-common/target \ No newline at end of file +enlish-framework/enlish-common/target +enlish-framework/enlish-spring-boot-starter-biz-operationlog/target \ No newline at end of file diff --git a/enlish-framework/enlish-spring-boot-starter-biz-operationlog/pom.xml b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/pom.xml new file mode 100644 index 0000000..38c3dc4 --- /dev/null +++ b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + + + com.yinlihupo + enlish-framework + ${revision} + + + + jar + + enlish-spring-boot-starter-biz-operationlog + ${project.artifactId} + 接口日志组件 + + + + com.yinlihupo + enlish-common + + + + + org.springframework.boot + spring-boot-starter-aop + + + + diff --git a/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/aspect/ApiOperationLog.java b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/aspect/ApiOperationLog.java new file mode 100644 index 0000000..88e7bed --- /dev/null +++ b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/aspect/ApiOperationLog.java @@ -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 ""; + +} + diff --git a/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/aspect/ApiOperationLogAspect.java b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/aspect/ApiOperationLogAspect.java new file mode 100644 index 0000000..983597f --- /dev/null +++ b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/aspect/ApiOperationLogAspect.java @@ -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 toJsonStr() { + return JsonUtils::toJsonString; + } + +} diff --git a/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/config/ApiOperationLogAutoConfiguration.java b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/config/ApiOperationLogAutoConfiguration.java new file mode 100644 index 0000000..5125048 --- /dev/null +++ b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/java/com/yinlihupo/framework/biz/operationlog/config/ApiOperationLogAutoConfiguration.java @@ -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(); + } +} diff --git a/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports new file mode 100644 index 0000000..897f2ba --- /dev/null +++ b/enlish-framework/enlish-spring-boot-starter-biz-operationlog/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports @@ -0,0 +1 @@ +com.yinlihupo.framework.biz.operationlog.config.ApiOperationLogAutoConfiguration \ No newline at end of file diff --git a/enlish-framework/pom.xml b/enlish-framework/pom.xml index 81e221c..9309c66 100644 --- a/enlish-framework/pom.xml +++ b/enlish-framework/pom.xml @@ -20,6 +20,7 @@ enlish-common + enlish-spring-boot-starter-biz-operationlog diff --git a/enlish-service/pom.xml b/enlish-service/pom.xml index cfae0c2..0fdd71c 100644 --- a/enlish-service/pom.xml +++ b/enlish-service/pom.xml @@ -24,6 +24,12 @@ enlish-common + + + com.yinlihupo + enlish-spring-boot-starter-biz-operationlog + + org.springframework.boot spring-boot-starter-web diff --git a/enlish-service/src/main/java/com/yinlihupo/enlish/service/deoms/web/TestController.java b/enlish-service/src/main/java/com/yinlihupo/enlish/service/deoms/web/TestController.java index 985d42c..b2eee7b 100644 --- a/enlish-service/src/main/java/com/yinlihupo/enlish/service/deoms/web/TestController.java +++ b/enlish-service/src/main/java/com/yinlihupo/enlish/service/deoms/web/TestController.java @@ -1,5 +1,6 @@ package com.yinlihupo.enlish.service.deoms.web; +import com.yinlihupo.framework.biz.operationlog.aspect.ApiOperationLog; import com.yinlihupo.framework.common.response.Response; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @@ -8,6 +9,7 @@ import org.springframework.web.bind.annotation.RestController; public class TestController { @GetMapping("/test") + @ApiOperationLog(description = "测试接口") public Response test() { return Response.success("Hello, 犬小哈专栏"); } diff --git a/pom.xml b/pom.xml index 8d1f7e2..950c9f0 100644 --- a/pom.xml +++ b/pom.xml @@ -70,6 +70,13 @@ ${revision} + + + com.yinlihupo + enlish-spring-boot-starter-biz-operationlog + ${revision} + + org.projectlombok