Skip to content

Commit

Permalink
refactor(log): 优化日志处理器解析 description、module 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Jan 2, 2025
1 parent eb2cac5 commit a6c9d33
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 版实现
Expand All @@ -29,20 +25,4 @@
* @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("请在该接口类上指定所属模块");
}
}
}
8 changes: 8 additions & 0 deletions continew-starter-log/continew-starter-log-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@

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;
Expand Down Expand Up @@ -85,6 +88,13 @@ public void logDescription(LogRecord logRecord, Method targetMethod) {
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 来指定日志描述");
}

/**
Expand All @@ -107,6 +117,13 @@ public void logModule(LogRecord logRecord, Method targetMethod, Class<?> targetC
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
Expand Down
6 changes: 0 additions & 6 deletions continew-starter-log/continew-starter-log-interceptor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* 日志处理器-拦截器版实现
Expand All @@ -32,31 +25,4 @@
* @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, "请在该接口类上指定所属模块"));
}
}
}

0 comments on commit a6c9d33

Please sign in to comment.