mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-09-11 06:57:12 +08:00
优化:优化日志表结构(新增 module 所属模块字段);优化日志引擎部分代码;使用 defaultIfNull() 和 blankToDefault 替换部分三元运算符代码(便于阅读及理解);将 BaseEntity 重命名为 BaseDO
This commit is contained in:
@@ -19,7 +19,7 @@ package top.charles7c.cnadmin.monitor.annotation;
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 系统日志注解(用于接口方法或类上)
|
||||
* 系统日志注解(用于接口方法或类上,辅助 Spring Doc OpenAPI3 使用效果最佳)
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2022/12/23 20:00
|
||||
@@ -30,12 +30,28 @@ import java.lang.annotation.*;
|
||||
public @interface Log {
|
||||
|
||||
/**
|
||||
* 日志描述
|
||||
* 日志描述(仅用于接口方法上)
|
||||
* <p>
|
||||
* 读取顺序:(越靠后优先级越高)<br>
|
||||
* 1、读取对应接口方法上的 @Operation(summary="描述") 内容<br>
|
||||
* 2、读取对应接口方法上的 @Log("描述") 内容<br>
|
||||
* </p>
|
||||
*/
|
||||
String value() default "";
|
||||
|
||||
/**
|
||||
* 是否忽略日志记录
|
||||
* 所属模块(用于接口方法或类上)
|
||||
* <p>
|
||||
* 读取顺序:(越靠后优先级越高)<br>
|
||||
* 1、读取对应接口类上的 @Tag(name = "模块") 内容<br>
|
||||
* 2、读取对应接口类上的 @Log(module = "模块") 内容<br>
|
||||
* 3、读取对应接口方法上的 @Log(module = "模块") 内容
|
||||
* </p>
|
||||
*/
|
||||
String module() default "";
|
||||
|
||||
/**
|
||||
* 是否忽略日志记录(仅用于接口方法上)
|
||||
*/
|
||||
boolean ignore() default false;
|
||||
}
|
||||
|
@@ -26,8 +26,8 @@ import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
@@ -39,6 +39,7 @@ import org.springframework.web.util.WebUtils;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.hutool.extra.servlet.ServletUtil;
|
||||
import cn.hutool.extra.spring.SpringUtil;
|
||||
@@ -88,8 +89,11 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
return;
|
||||
}
|
||||
|
||||
HandlerMethod handlerMethod = (HandlerMethod)handler;
|
||||
// 记录所属模块
|
||||
this.logModule(logDO, handlerMethod);
|
||||
// 记录日志描述
|
||||
this.logDescription(logDO, handler);
|
||||
this.logDescription(logDO, handlerMethod);
|
||||
// 记录请求信息
|
||||
this.logRequest(logDO, request);
|
||||
// 记录响应信息
|
||||
@@ -140,24 +144,51 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录所属模块
|
||||
*
|
||||
* @param logDO
|
||||
* 系统日志信息
|
||||
* @param handlerMethod
|
||||
* 处理器方法
|
||||
*/
|
||||
private void logModule(LogDO logDO, HandlerMethod handlerMethod) {
|
||||
Tag classTag = handlerMethod.getBeanType().getDeclaredAnnotation(Tag.class);
|
||||
Log classLog = handlerMethod.getBeanType().getDeclaredAnnotation(Log.class);
|
||||
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
|
||||
|
||||
// 例如:@Tag(name = "部门管理") -> 部门管理
|
||||
// (本框架代码规范)例如:@Tag(name = "部门管理 API") -> 部门管理
|
||||
if (classTag != null) {
|
||||
String name = classTag.name();
|
||||
logDO.setModule(StrUtil.isNotBlank(name) ? name.replace("API", "").trim() : "请在该接口类上指定所属模块");
|
||||
}
|
||||
// 例如:@Log(module = "部门管理") -> 部门管理
|
||||
if (classLog != null && StrUtil.isNotBlank(classLog.module())) {
|
||||
logDO.setModule(classLog.module());
|
||||
}
|
||||
if (methodLog != null && StrUtil.isNotBlank(methodLog.module())) {
|
||||
logDO.setModule(methodLog.module());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 记录日志描述
|
||||
*
|
||||
* @param logDO
|
||||
* 系统日志信息
|
||||
* @param handler
|
||||
* 处理器
|
||||
* @param handlerMethod
|
||||
* 处理器方法
|
||||
*/
|
||||
private void logDescription(LogDO logDO, Object handler) {
|
||||
HandlerMethod handlerMethod = (HandlerMethod)handler;
|
||||
Operation methodOperation = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Operation.class);
|
||||
Log methodLog = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Log.class);
|
||||
private void logDescription(LogDO logDO, HandlerMethod handlerMethod) {
|
||||
Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class);
|
||||
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
|
||||
|
||||
// 例如:@Operation(summary="新增部门") -> 新增部门
|
||||
if (methodOperation != null) {
|
||||
logDO.setDescription(
|
||||
StrUtil.isNotBlank(methodOperation.summary()) ? methodOperation.summary() : "请在该接口方法上指定日志描述");
|
||||
logDO.setDescription(StrUtil.blankToDefault(methodOperation.summary(), "请在该接口方法上指定日志描述"));
|
||||
}
|
||||
// 例如:@Log("获取验证码") -> 获取验证码
|
||||
// 例如:@Log("新增部门") -> 新增部门
|
||||
if (methodLog != null && StrUtil.isNotBlank(methodLog.value())) {
|
||||
logDO.setDescription(methodLog.value());
|
||||
}
|
||||
@@ -184,7 +215,7 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
logDO.setClientIp(ServletUtil.getClientIP(request));
|
||||
logDO.setLocation(IpUtils.getCityInfo(logDO.getClientIp()));
|
||||
logDO.setBrowser(ServletUtils.getBrowser(request));
|
||||
logDO.setCreateUser(logDO.getCreateUser() == null ? LoginHelper.getUserId() : logDO.getCreateUser());
|
||||
logDO.setCreateUser(ObjectUtil.defaultIfNull(logDO.getCreateUser(), LoginHelper.getUserId()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -290,13 +321,13 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
|
||||
// 3、排除不需要记录系统日志的接口
|
||||
HandlerMethod handlerMethod = (HandlerMethod)handler;
|
||||
Log methodLog = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Log.class);
|
||||
// 3.1 请求方式不要求记录且请求上没有 @Log 注解,则不记录系统日志
|
||||
Log methodLog = handlerMethod.getMethodAnnotation(Log.class);
|
||||
// 3.1 请求方式不要求记录且接口方法上没有 @Log 注解,则不记录系统日志
|
||||
if (operationLogProperties.getExcludeMethods().contains(request.getMethod()) && methodLog == null) {
|
||||
return false;
|
||||
}
|
||||
// 3.2 如果接口上既没有 @Log 注解,也没有 @Operation 注解,则不记录系统日志
|
||||
Operation methodOperation = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), Operation.class);
|
||||
// 3.2 如果接口方法上既没有 @Log 注解,也没有 @Operation 注解,则不记录系统日志
|
||||
Operation methodOperation = handlerMethod.getMethodAnnotation(Operation.class);
|
||||
if (methodLog == null && methodOperation == null) {
|
||||
return false;
|
||||
}
|
||||
@@ -304,7 +335,7 @@ public class LogInterceptor implements HandlerInterceptor {
|
||||
if (methodOperation != null && methodOperation.hidden()) {
|
||||
return false;
|
||||
}
|
||||
// 3.4 如果接口上有 @Log 注解,但是要求忽略该接口,则不记录系统日志
|
||||
// 3.4 如果接口方法上有 @Log 注解,但是要求忽略该接口,则不记录系统日志
|
||||
return methodLog == null || !methodLog.ignore();
|
||||
}
|
||||
}
|
||||
|
@@ -49,6 +49,11 @@ public class LogDO implements Serializable {
|
||||
*/
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 所属模块
|
||||
*/
|
||||
private String module;
|
||||
|
||||
/**
|
||||
* 请求URL
|
||||
*/
|
||||
|
@@ -16,6 +16,9 @@
|
||||
|
||||
package top.charles7c.cnadmin.monitor.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
@@ -29,7 +32,9 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
* @since 2023/1/17 21:43
|
||||
*/
|
||||
@Data
|
||||
public class LogVO {
|
||||
public class LogVO implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 创建人
|
||||
@@ -42,4 +47,10 @@ public class LogVO {
|
||||
*/
|
||||
@Schema(description = "创建人")
|
||||
private String createUserString;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Schema(description = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
|
@@ -16,9 +16,6 @@
|
||||
|
||||
package top.charles7c.cnadmin.monitor.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
@@ -33,7 +30,7 @@ import top.charles7c.cnadmin.monitor.enums.LogStatusEnum;
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "登录日志信息")
|
||||
public class LoginLogVO extends LogVO implements Serializable {
|
||||
public class LoginLogVO extends LogVO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -78,10 +75,4 @@ public class LoginLogVO extends LogVO implements Serializable {
|
||||
*/
|
||||
@Schema(description = "错误信息")
|
||||
private String errorMsg;
|
||||
|
||||
/**
|
||||
* 登录时间
|
||||
*/
|
||||
@Schema(description = "登录时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
|
@@ -16,9 +16,6 @@
|
||||
|
||||
package top.charles7c.cnadmin.monitor.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
@@ -33,7 +30,7 @@ import top.charles7c.cnadmin.monitor.enums.LogStatusEnum;
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "操作日志信息")
|
||||
public class OperationLogVO extends LogVO implements Serializable {
|
||||
public class OperationLogVO extends LogVO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -49,6 +46,12 @@ public class OperationLogVO extends LogVO implements Serializable {
|
||||
@Schema(description = "操作内容")
|
||||
private String description;
|
||||
|
||||
/**
|
||||
* 所属模块
|
||||
*/
|
||||
@Schema(description = "所属模块")
|
||||
private String module;
|
||||
|
||||
/**
|
||||
* 操作状态(1成功 2失败)
|
||||
*/
|
||||
@@ -78,10 +81,4 @@ public class OperationLogVO extends LogVO implements Serializable {
|
||||
*/
|
||||
@Schema(description = "错误信息")
|
||||
private String errorMsg;
|
||||
|
||||
/**
|
||||
* 操作时间
|
||||
*/
|
||||
@Schema(description = "操作时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
|
@@ -16,9 +16,6 @@
|
||||
|
||||
package top.charles7c.cnadmin.monitor.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
@@ -31,7 +28,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "系统日志详情信息")
|
||||
public class SystemLogDetailVO extends LogVO implements Serializable {
|
||||
public class SystemLogDetailVO extends LogVO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -112,10 +109,4 @@ public class SystemLogDetailVO extends LogVO implements Serializable {
|
||||
*/
|
||||
@Schema(description = "浏览器")
|
||||
private String browser;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Schema(description = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
|
@@ -16,9 +16,6 @@
|
||||
|
||||
package top.charles7c.cnadmin.monitor.model.vo;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
@@ -31,7 +28,7 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "系统日志信息")
|
||||
public class SystemLogVO extends LogVO implements Serializable {
|
||||
public class SystemLogVO extends LogVO {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -100,10 +97,4 @@ public class SystemLogVO extends LogVO implements Serializable {
|
||||
*/
|
||||
@Schema(description = "异常详情")
|
||||
private String exceptionDetail;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
@Schema(description = "创建时间")
|
||||
private LocalDateTime createTime;
|
||||
}
|
||||
|
Reference in New Issue
Block a user