feat(syslog): 操作日志新增操作结果列,记录成功失败并追加失败原因
- AutoLogAspect around() 改造为 try/catch/finally:抛异常或返回 Result.error 判定失败,异常不吞保持原行为 - 失败原因截断 500 字符追加到 log_content - sys_log 新增 operate_result 列(1成功,0失败)
This commit is contained in:
@@ -66,6 +66,11 @@ public class LogDTO implements Serializable {
|
||||
*/
|
||||
private Integer userSecurityLevel;
|
||||
|
||||
/**
|
||||
* 操作结果(1成功,0失败)
|
||||
*/
|
||||
private Integer operateResult;
|
||||
|
||||
public LogDTO(){
|
||||
|
||||
}
|
||||
|
||||
@@ -78,18 +78,36 @@ public class AutoLogAspect {
|
||||
@Around("logPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
long beginTime = System.currentTimeMillis();
|
||||
//执行方法
|
||||
Object result = point.proceed();
|
||||
//执行时长(毫秒)
|
||||
long time = System.currentTimeMillis() - beginTime;
|
||||
|
||||
//保存日志
|
||||
saveSysLog(point, time, result);
|
||||
|
||||
boolean success = true;
|
||||
String errorMsg = null;
|
||||
Object result = null;
|
||||
try {
|
||||
//执行方法
|
||||
result = point.proceed();
|
||||
} catch (Exception e) {
|
||||
//方法抛异常:记录失败,但不吞异常,保持原业务行为
|
||||
success = false;
|
||||
errorMsg = e.getMessage();
|
||||
throw e;
|
||||
} finally {
|
||||
//update-begin-author:wsm date:20260805 for:判定操作结果,返回值 Result.error 也视为失败
|
||||
if (success && result instanceof Result) {
|
||||
Result<?> res = (Result<?>) result;
|
||||
if (!res.isSuccess()) {
|
||||
success = false;
|
||||
errorMsg = res.getMessage();
|
||||
}
|
||||
}
|
||||
//update-end-author:wsm date:20260805 for:判定操作结果,返回值 Result.error 也视为失败
|
||||
//执行时长(毫秒)
|
||||
long time = System.currentTimeMillis() - beginTime;
|
||||
//保存日志
|
||||
saveSysLog(point, time, success, errorMsg, result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private void saveSysLog(ProceedingJoinPoint joinPoint, long time, Object obj) {
|
||||
private void saveSysLog(ProceedingJoinPoint joinPoint, long time, boolean success, String errorMsg, Object obj) {
|
||||
try {
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
@@ -107,6 +125,15 @@ public class AutoLogAspect {
|
||||
dto.setLogContent(content);
|
||||
}
|
||||
|
||||
//update-begin-author:wsm date:20260805 for:记录操作结果,失败原因追加到日志内容
|
||||
dto.setOperateResult(success ? CommonConstant.OPERATE_RESULT_SUCCESS : CommonConstant.OPERATE_RESULT_FAIL);
|
||||
if (!success && oConvertUtils.isNotEmpty(errorMsg)) {
|
||||
String reason = errorMsg.length() > 500 ? errorMsg.substring(0, 500) : errorMsg;
|
||||
String content = dto.getLogContent() == null ? "" : dto.getLogContent();
|
||||
dto.setLogContent(content + ",失败原因:" + reason);
|
||||
}
|
||||
//update-end-author:wsm date:20260805 for:记录操作结果,失败原因追加到日志内容
|
||||
|
||||
//请求的方法名
|
||||
String className = joinPoint.getTarget().getClass().getName();
|
||||
String methodName = signature.getName();
|
||||
|
||||
@@ -75,8 +75,18 @@ public interface CommonConstant {
|
||||
* 操作日志类型: 导出
|
||||
*/
|
||||
int OPERATE_TYPE_6 = 6;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 操作结果: 成功
|
||||
*/
|
||||
int OPERATE_RESULT_SUCCESS = 1;
|
||||
|
||||
/**
|
||||
* 操作结果: 失败
|
||||
*/
|
||||
int OPERATE_RESULT_FAIL = 0;
|
||||
|
||||
|
||||
/** {@code 500 Server Error} (HTTP/1.0 - RFC 1945) */
|
||||
Integer SC_INTERNAL_SERVER_ERROR_500 = 500;
|
||||
/** {@code 404 Not Found} (HTTP/1.0 - RFC 1945) */
|
||||
|
||||
+2
-1
@@ -4,13 +4,14 @@
|
||||
|
||||
<!-- 保存日志11 -->
|
||||
<insert id="saveLog" parameterType="Object">
|
||||
insert into sys_log (id, log_type, log_content, method, operate_type, request_url, request_type, request_param, ip, userid, username, cost_time, create_time,create_by, tenant_id, client_type, user_security_level)
|
||||
insert into sys_log (id, log_type, log_content, method, operate_type, operate_result, request_url, request_type, request_param, ip, userid, username, cost_time, create_time,create_by, tenant_id, client_type, user_security_level)
|
||||
values(
|
||||
#{dto.id,jdbcType=VARCHAR},
|
||||
#{dto.logType,jdbcType=INTEGER},
|
||||
#{dto.logContent,jdbcType=VARCHAR},
|
||||
#{dto.method,jdbcType=VARCHAR},
|
||||
#{dto.operateType,jdbcType=INTEGER},
|
||||
#{dto.operateResult,jdbcType=INTEGER},
|
||||
#{dto.requestUrl,jdbcType=VARCHAR},
|
||||
#{dto.requestType,jdbcType=VARCHAR},
|
||||
#{dto.requestParam,jdbcType=VARCHAR},
|
||||
|
||||
+5
@@ -125,4 +125,9 @@ public class SysLog implements Serializable {
|
||||
*/
|
||||
private Integer userSecurityLevel;
|
||||
|
||||
/**
|
||||
* 操作结果(1成功,0失败)
|
||||
*/
|
||||
private Integer operateResult;
|
||||
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ YYYYMMDD_描述.sql
|
||||
| 2026-07-09 | DML | `20260709_新增督办状态字典.sql` | 新增 super_status 字典(未开始/督办中/已完成) |
|
||||
| 2026-07-09 | DML | `20260709_新增反馈任务状态字典.sql` | 新增 feedback_status 字典(未开始/进行中/已完成) |
|
||||
| 2026-08-05 | DDL | `20260805_系统日志表新增操作人密级字段.sql` | sys_log 新增 user_security_level 列,记录操作人密级 |
|
||||
| 2026-08-05 | DDL | `20260805_系统日志表新增操作结果字段.sql` | sys_log 新增 operate_result 列,记录操作日志成功/失败 |
|
||||
|
||||
## 使用方式
|
||||
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
-- 系统日志表新增操作结果字段
|
||||
-- @AutoLog 标注的方法执行成功/失败时记录:1成功,0失败
|
||||
-- 失败原因追加到 log_content
|
||||
-- 变更日期:2026-08-05
|
||||
ALTER TABLE `sys_log`
|
||||
ADD COLUMN `operate_result` tinyint(1) NULL DEFAULT NULL COMMENT '操作结果(1成功,0失败)' AFTER `operate_type`;
|
||||
Reference in New Issue
Block a user