mirror of
				https://github.com/continew-org/continew-starter.git
				synced 2025-10-26 19:00:53 +08:00 
			
		
		
		
	refactor(log): 优化日志处理器解析 description、module 方法
This commit is contained in:
		| @@ -16,11 +16,7 @@ | ||||
|  | ||||
| package top.continew.starter.log.handler; | ||||
|  | ||||
| import cn.hutool.core.text.CharSequenceUtil; | ||||
| import top.continew.starter.log.AbstractLogHandler; | ||||
| import top.continew.starter.log.model.LogRecord; | ||||
|  | ||||
| import java.lang.reflect.Method; | ||||
|  | ||||
| /** | ||||
|  * 日志处理器-AOP 版实现 | ||||
| @@ -29,20 +25,4 @@ import java.lang.reflect.Method; | ||||
|  * @since 2.8.0 | ||||
|  */ | ||||
| public class AopLogHandler extends AbstractLogHandler { | ||||
|  | ||||
|     @Override | ||||
|     public void logDescription(LogRecord logRecord, Method targetMethod) { | ||||
|         super.logDescription(logRecord, targetMethod); | ||||
|         if (CharSequenceUtil.isBlank(logRecord.getDescription())) { | ||||
|             logRecord.setDescription("请在该接口方法上指定日志描述"); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void logModule(LogRecord logRecord, Method targetMethod, Class<?> targetClass) { | ||||
|         super.logModule(logRecord, targetMethod, targetClass); | ||||
|         if (CharSequenceUtil.isBlank(logRecord.getModule())) { | ||||
|             logRecord.setModule("请在该接口类上指定所属模块"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -11,4 +11,12 @@ | ||||
|  | ||||
|     <artifactId>continew-starter-log-core</artifactId> | ||||
|     <description>ContiNew Starter 日志模块 - 核心模块</description> | ||||
|  | ||||
|     <dependencies> | ||||
|         <!-- Swagger 注解 --> | ||||
|         <dependency> | ||||
|             <groupId>io.swagger.core.v3</groupId> | ||||
|             <artifactId>swagger-annotations-jakarta</artifactId> | ||||
|         </dependency> | ||||
|     </dependencies> | ||||
| </project> | ||||
| @@ -18,8 +18,11 @@ package top.continew.starter.log; | ||||
|  | ||||
| import cn.hutool.core.annotation.AnnotationUtil; | ||||
| import cn.hutool.core.text.CharSequenceUtil; | ||||
| import io.swagger.v3.oas.annotations.Operation; | ||||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||||
| import jakarta.servlet.http.HttpServletRequest; | ||||
| import jakarta.servlet.http.HttpServletResponse; | ||||
| import top.continew.starter.core.validation.ValidationUtils; | ||||
| import top.continew.starter.log.annotation.Log; | ||||
| import top.continew.starter.log.enums.Include; | ||||
| import top.continew.starter.log.http.servlet.RecordableServletHttpRequest; | ||||
| @@ -85,6 +88,13 @@ public abstract class AbstractLogHandler implements LogHandler { | ||||
|         if (null != methodLog && CharSequenceUtil.isNotBlank(methodLog.value())) { | ||||
|             logRecord.setDescription(methodLog.value()); | ||||
|         } | ||||
|         // 例如:@Operation(summary="新增部门") -> 新增部门 | ||||
|         Operation methodOperation = AnnotationUtil.getAnnotation(targetMethod, Operation.class); | ||||
|         if (null != methodOperation) { | ||||
|             logRecord.setDescription(CharSequenceUtil.blankToDefault(methodOperation | ||||
|                 .summary(), "请在该接口方法的 @Operation 上添加 summary 来指定日志描述")); | ||||
|         } | ||||
|         ValidationUtils.throwIfBlank(logRecord.getDescription(), "请在该接口方法上添加 @Log 来指定日志描述"); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
| @@ -107,6 +117,13 @@ public abstract class AbstractLogHandler implements LogHandler { | ||||
|         if (null != classLog && CharSequenceUtil.isNotBlank(classLog.module())) { | ||||
|             logRecord.setModule(classLog.module()); | ||||
|         } | ||||
|         // 例如:@Tag(name = "部门管理") -> 部门管理 | ||||
|         Tag classTag = AnnotationUtil.getAnnotation(targetClass, Tag.class); | ||||
|         if (null != classTag) { | ||||
|             String name = classTag.name(); | ||||
|             logRecord.setModule(CharSequenceUtil.blankToDefault(name, "请在该接口类的 @Tag 上添加 name 来指定所属模块")); | ||||
|         } | ||||
|         ValidationUtils.throwIfBlank(logRecord.getModule(), "请在该接口方法或接口类上添加 @Log 来指定所属模块"); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
| @@ -13,12 +13,6 @@ | ||||
|     <description>ContiNew Starter 日志模块 - 基于拦截器实现(Spring Boot Actuator HttpTrace 增强版)</description> | ||||
|  | ||||
|     <dependencies> | ||||
|         <!-- Swagger 注解 --> | ||||
|         <dependency> | ||||
|             <groupId>io.swagger.core.v3</groupId> | ||||
|             <artifactId>swagger-annotations-jakarta</artifactId> | ||||
|         </dependency> | ||||
|  | ||||
|         <!-- TTL(线程间传递 ThreadLocal,异步执行时上下文传递的解决方案) --> | ||||
|         <dependency> | ||||
|             <groupId>com.alibaba</groupId> | ||||
|   | ||||
| @@ -16,14 +16,7 @@ | ||||
|  | ||||
| package top.continew.starter.log.handler; | ||||
|  | ||||
| import cn.hutool.core.annotation.AnnotationUtil; | ||||
| import cn.hutool.core.text.CharSequenceUtil; | ||||
| import io.swagger.v3.oas.annotations.Operation; | ||||
| import io.swagger.v3.oas.annotations.tags.Tag; | ||||
| import top.continew.starter.log.AbstractLogHandler; | ||||
| import top.continew.starter.log.model.LogRecord; | ||||
|  | ||||
| import java.lang.reflect.Method; | ||||
|  | ||||
| /** | ||||
|  * 日志处理器-拦截器版实现 | ||||
| @@ -32,31 +25,4 @@ import java.lang.reflect.Method; | ||||
|  * @since 2.8.0 | ||||
|  */ | ||||
| public class InterceptorLogHandler extends AbstractLogHandler { | ||||
|  | ||||
|     @Override | ||||
|     public void logDescription(LogRecord logRecord, Method targetMethod) { | ||||
|         super.logDescription(logRecord, targetMethod); | ||||
|         if (CharSequenceUtil.isNotBlank(logRecord.getDescription())) { | ||||
|             return; | ||||
|         } | ||||
|         // 例如:@Operation(summary="新增部门") -> 新增部门 | ||||
|         Operation methodOperation = AnnotationUtil.getAnnotation(targetMethod, Operation.class); | ||||
|         if (null != methodOperation) { | ||||
|             logRecord.setDescription(CharSequenceUtil.blankToDefault(methodOperation.summary(), "请在该接口方法上指定日志描述")); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void logModule(LogRecord logRecord, Method targetMethod, Class<?> targetClass) { | ||||
|         super.logModule(logRecord, targetMethod, targetClass); | ||||
|         if (CharSequenceUtil.isNotBlank(logRecord.getModule())) { | ||||
|             return; | ||||
|         } | ||||
|         // 例如:@Tag(name = "部门管理") -> 部门管理 | ||||
|         Tag classTag = AnnotationUtil.getAnnotation(targetClass, Tag.class); | ||||
|         if (null != classTag) { | ||||
|             String name = classTag.name(); | ||||
|             logRecord.setModule(CharSequenceUtil.blankToDefault(name, "请在该接口类上指定所属模块")); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user