refactor(log): Log 注解新增 include、exclude 属性,用于扩展或减少日志包含信息

处理类上 Log 注解的日志包含信息 -> 处理方法上 Log 注解的日志包含信息
This commit is contained in:
2024-02-05 23:27:10 +08:00
parent 18b9d1ba79
commit 669ea85658
2 changed files with 58 additions and 9 deletions

View File

@@ -16,6 +16,8 @@
package top.charles7c.continew.starter.log.common.annotation;
import top.charles7c.continew.starter.log.common.enums.Include;
import java.lang.annotation.*;
/**
@@ -46,6 +48,16 @@ public @interface Log {
*/
String module() default "";
/**
* 包含信息(在全局配置基础上扩展包含信息)
*/
Include[] include() default {};
/**
* 排除信息(在全局配置基础上减少包含信息)
*/
Include[] exclude() default {};
/**
* 是否忽略日志记录(用于接口方法或类上)
*/

View File

@@ -83,18 +83,20 @@ public class LogInterceptor implements HandlerInterceptor {
return;
}
timestampTtl.remove();
Set<Include> includeSet = logProperties.getInclude();
try {
HandlerMethod handlerMethod = (HandlerMethod)handler;
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
Log classLog = handlerMethod.getBeanType().getDeclaredAnnotation(Log.class);
Set<Include> includeSet = this.getIncludes(methodLog, classLog);
LogRecord finishedLogRecord = startedLogRecord.finish(new RecordableServletHttpResponse(response, response
.getStatus()), includeSet);
HandlerMethod handlerMethod = (HandlerMethod)handler;
// 记录日志描述
if (includeSet.contains(Include.DESCRIPTION)) {
this.logDescription(finishedLogRecord, handlerMethod);
this.logDescription(finishedLogRecord, methodLog, handlerMethod);
}
// 记录所属模块
if (includeSet.contains(Include.MODULE)) {
this.logModule(finishedLogRecord, handlerMethod);
this.logModule(finishedLogRecord, methodLog, classLog, handlerMethod);
}
if (Boolean.TRUE.equals(logProperties.getIsPrint())) {
LogResponse logResponse = finishedLogRecord.getResponse();
@@ -107,15 +109,50 @@ public class LogInterceptor implements HandlerInterceptor {
}
}
/**
* 获取日志包含信息
*
* @param methodLog 方法级 Log 注解
* @param classLog 类级 Log 注解
* @return 日志包含信息
*/
private Set<Include> getIncludes(Log methodLog, Log classLog) {
Set<Include> includeSet = logProperties.getInclude();
if (null != classLog) {
this.processInclude(includeSet, classLog);
}
if (null != methodLog) {
this.processInclude(includeSet, methodLog);
}
return includeSet;
}
/**
* 处理日志包含信息
*
* @param includes 日志包含信息
* @param logAnnotation Log 注解
*/
private void processInclude(Set<Include> includes, Log logAnnotation) {
Include[] includeArr = logAnnotation.include();
if (includeArr.length > 0) {
includes.addAll(Set.of(includeArr));
}
Include[] excludeArr = logAnnotation.exclude();
if (excludeArr.length > 0) {
includes.removeAll(Set.of(excludeArr));
}
}
/**
* 记录描述
*
* @param logRecord 日志信息
* @param methodLog 方法级 Log 注解
* @param handlerMethod 处理器方法
*/
private void logDescription(LogRecord logRecord, HandlerMethod handlerMethod) {
private void logDescription(LogRecord logRecord, Log methodLog, HandlerMethod handlerMethod) {
// 例如:@Log("新增部门") -> 新增部门
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
if (null != methodLog && StrUtil.isNotBlank(methodLog.value())) {
logRecord.setDescription(methodLog.value());
return;
@@ -131,16 +168,16 @@ public class LogInterceptor implements HandlerInterceptor {
* 记录模块
*
* @param logRecord 日志信息
* @param methodLog 方法级 Log 注解
* @param classLog 类级 Log 注解
* @param handlerMethod 处理器方法
*/
private void logModule(LogRecord logRecord, HandlerMethod handlerMethod) {
private void logModule(LogRecord logRecord, Log methodLog, Log classLog, HandlerMethod handlerMethod) {
// 例如:@Log(module = "部门管理") -> 部门管理
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
if (null != methodLog && StrUtil.isNotBlank(methodLog.module())) {
logRecord.setModule(methodLog.module());
return;
}
Log classLog = handlerMethod.getBeanType().getDeclaredAnnotation(Log.class);
if (null != classLog && StrUtil.isNotBlank(classLog.module())) {
logRecord.setModule(classLog.module());
return;