han-note项目初始化完毕!
- 整合完毕MyBatis-Plus - 整合Druid数据库链接池 - 自定义两个Starter,业务日志打印Starter和自定义Jackson配置类,支持Java8的LocalTime等API - 整合 flatten-maven-plugin 插件:解决子模块单独打包失败问题
This commit is contained in:
31
hanserwei-framework/hanserwei-spring-boot-starter-biz-operationlog/pom.xml
Executable file
31
hanserwei-framework/hanserwei-spring-boot-starter-biz-operationlog/pom.xml
Executable file
@@ -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.hanserwei</groupId>
|
||||
<artifactId>hanserwei-framework</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<artifactId>hanserwei-spring-boot-starter-biz-operationlog</artifactId>
|
||||
<name>${project.artifactId}</name>
|
||||
<description>接口日志组件</description>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.hanserwei</groupId>
|
||||
<artifactId>hanserwei-common</artifactId>
|
||||
</dependency>
|
||||
<!-- AOP 切面 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.hanserwei.framework.biz.operationlog.aspect;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.METHOD)
|
||||
@Documented
|
||||
public @interface ApiOperationLog {
|
||||
/**
|
||||
* API 功能描述
|
||||
*
|
||||
* @return API 功能描述
|
||||
*/
|
||||
String description() default "";
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package com.hanserwei.framework.biz.operationlog.aspect;
|
||||
|
||||
import com.hanserwei.framework.common.utils.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.hanserwei.framework.biz.operationlog.aspect.ApiOperationLog)")
|
||||
public void apiOperationLog() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 环绕
|
||||
*
|
||||
* @param joinPoint 切入点
|
||||
* @return 方法执行结果
|
||||
* @throws Throwable 抛出异常
|
||||
*/
|
||||
@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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取注解的描述信息
|
||||
*
|
||||
* @param joinPoint 切点
|
||||
* @return 描述信息
|
||||
*/
|
||||
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 字符串
|
||||
*
|
||||
* @return Function
|
||||
*/
|
||||
private Function<Object, String> toJsonStr() {
|
||||
return JsonUtils::toJsonString;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.hanserwei.framework.biz.operationlog.config;
|
||||
|
||||
import com.hanserwei.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.hanserwei.framework.biz.operationlog.config.ApiOperationLogAutoConfiguration
|
||||
Reference in New Issue
Block a user