操作日志新增消耗时间属性

This commit is contained in:
RuoYi 2023-02-16 10:23:14 +08:00
parent e8043769aa
commit 47f63584ef
4 changed files with 41 additions and 2 deletions

View File

@ -571,7 +571,8 @@ create table sys_oper_log (
oper_param varchar2(2000) default '',
json_result varchar2(2000) default '',
status number(1) default 0,
error_msg varchar2(2000) default '' ,
error_msg varchar2(2000) default '',
cost_time number(20) default 0,
oper_time date
);
@ -593,6 +594,7 @@ comment on column sys_oper_log.oper_param is '请求参数';
comment on column sys_oper_log.json_result is '返回参数';
comment on column sys_oper_log.status is '操作状态0正常 1异常';
comment on column sys_oper_log.error_msg is '错误消息';
comment on column sys_oper_log.cost_time is '消耗时间';
comment on column sys_oper_log.oper_time is '操作时间';

View File

@ -8,8 +8,10 @@ import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.NamedThreadLocal;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
@ -41,6 +43,18 @@ public class LogAspect
/** 排除敏感属性字段 */
public static final String[] EXCLUDE_PROPERTIES = { "password", "oldPassword", "newPassword", "confirmPassword" };
/** 计算操作消耗时间 */
private static final ThreadLocal<Long> TIME_THREADLOCAL = new NamedThreadLocal<Long>("Cost Time");
/**
* 处理请求前执行
*/
@Before(value = "@annotation(controllerLog)")
public void boBefore(JoinPoint joinPoint, Log controllerLog)
{
TIME_THREADLOCAL.set(System.currentTimeMillis());
}
/**
* 处理完请求后执行
*
@ -96,6 +110,8 @@ public class LogAspect
operLog.setRequestMethod(ServletUtils.getRequest().getMethod());
// 处理设置注解上的参数
getControllerMethodDescription(joinPoint, controllerLog, operLog, jsonResult);
// 设置消耗时间
operLog.setCostTime(System.currentTimeMillis() - TIME_THREADLOCAL.get());
// 保存数据库
AsyncManager.me().execute(AsyncFactory.recordOper(operLog));
}
@ -105,6 +121,10 @@ public class LogAspect
log.error("异常信息:{}", exp.getMessage());
exp.printStackTrace();
}
finally
{
TIME_THREADLOCAL.remove();
}
}
/**

View File

@ -83,6 +83,10 @@ public class SysOperLog extends BaseEntity
@Excel(name = "操作时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss")
private Date operTime;
/** 消耗时间 */
@Excel(name = "消耗时间", suffix = "毫秒")
private Long costTime;
public Long getOperId()
{
return operId;
@ -252,4 +256,14 @@ public class SysOperLog extends BaseEntity
{
this.operTime = operTime;
}
public Long getCostTime()
{
return costTime;
}
public void setCostTime(Long costTime)
{
this.costTime = costTime;
}
}

View File

@ -20,11 +20,12 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<result property="jsonResult" column="json_result" />
<result property="status" column="status" />
<result property="errorMsg" column="error_msg" />
<result property="costTime" column="cost_time" />
<result property="operTime" column="oper_time" />
</resultMap>
<sql id="selectOperLogVo">
select oper_id, title, business_type, method, request_method, operator_type, oper_name, dept_name, oper_url, oper_ip, oper_location, oper_param, json_result, status, error_msg, oper_time
select oper_id, title, business_type, method, request_method, operator_type, oper_name, dept_name, oper_url, oper_ip, oper_location, oper_param, json_result, status, error_msg, cost_time, oper_time
from sys_oper_log
</sql>
@ -48,6 +49,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="jsonResult != null and jsonResult != ''">json_result,</if>
<if test="status != null and status != ''">status,</if>
<if test="errorMsg != null and errorMsg != ''">error_msg,</if>
<if test="costTime != null and costTime != ''">cost_time,</if>
oper_time
)values(
<if test="operId != null and operId != 0">#{operId},</if>
@ -65,6 +67,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="jsonResult != null and jsonResult != ''">#{jsonResult},</if>
<if test="status != null and status != ''">#{status},</if>
<if test="errorMsg != null and errorMsg != ''">#{errorMsg},</if>
<if test="costTime != null and costTime != ''">#{costTime},</if>
sysdate
)
</insert>