From 669ea85658c89c631518def8f84d4f5d60059ad7 Mon Sep 17 00:00:00 2001 From: Charles7c Date: Mon, 5 Feb 2024 23:27:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor(log):=20Log=20=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20include=E3=80=81exclude=20=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=EF=BC=8C=E7=94=A8=E4=BA=8E=E6=89=A9=E5=B1=95=E6=88=96?= =?UTF-8?q?=E5=87=8F=E5=B0=91=E6=97=A5=E5=BF=97=E5=8C=85=E5=90=AB=E4=BF=A1?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 处理类上 Log 注解的日志包含信息 -> 处理方法上 Log 注解的日志包含信息 --- .../starter/log/common/annotation/Log.java | 12 ++++ .../httptracepro/handler/LogInterceptor.java | 55 ++++++++++++++++--- 2 files changed, 58 insertions(+), 9 deletions(-) diff --git a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/annotation/Log.java b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/annotation/Log.java index 48b0794b..c48cf2f1 100644 --- a/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/annotation/Log.java +++ b/continew-starter-log/continew-starter-log-common/src/main/java/top/charles7c/continew/starter/log/common/annotation/Log.java @@ -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 {}; + /** * 是否忽略日志记录(用于接口方法或类上) */ diff --git a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java index 00f58357..af785e5d 100644 --- a/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java +++ b/continew-starter-log/continew-starter-log-httptrace-pro/src/main/java/top/charles7c/continew/starter/log/httptracepro/handler/LogInterceptor.java @@ -83,18 +83,20 @@ public class LogInterceptor implements HandlerInterceptor { return; } timestampTtl.remove(); - Set includeSet = logProperties.getInclude(); try { + HandlerMethod handlerMethod = (HandlerMethod)handler; + Log methodLog = handlerMethod.getMethodAnnotation(Log.class); + Log classLog = handlerMethod.getBeanType().getDeclaredAnnotation(Log.class); + Set 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 getIncludes(Log methodLog, Log classLog) { + Set 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 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;