访问日志记录,增加表单提交前后的差异数据分析

This commit is contained in:
thinkgem
2020-12-22 00:16:30 +08:00
parent 7e42620f44
commit a96991f7ea
3 changed files with 40 additions and 5 deletions

View File

@@ -36,6 +36,7 @@ import com.jeesite.common.mybatis.mapper.query.QueryType;
@Column(name="request_uri", attrName="requestUri", label="请求URI", queryType=QueryType.LIKE),
@Column(name="request_method", attrName="requestMethod", label="操作方式"),
@Column(name="request_params", attrName="requestParams", label="操作提交的数据", queryType=QueryType.LIKE),
@Column(name="diff_modify_data",attrName="diffModifyData", label="差异修改数据", queryType=QueryType.LIKE),
@Column(name="biz_key", attrName="bizKey", label="业务主键"),
@Column(name="biz_type", attrName="bizType", label="业务类型"),
@Column(name="remote_addr", attrName="remoteAddr", label="操作IP地址"),
@@ -63,6 +64,7 @@ public class Log extends DataEntity<Log> {
private String requestUri; // 请求URI
private String requestMethod; // 操作方式
private String requestParams; // 操作提交的数据
private String diffModifyData; // 差异修改数据
private String bizKey; // 业务主键
private String bizType; // 业务类型
private String remoteAddr; // 操作IP地址
@@ -130,6 +132,14 @@ public class Log extends DataEntity<Log> {
this.requestParams = requestParams;
}
public String getDiffModifyData() {
return diffModifyData;
}
public void setDiffModifyData(String diffModifyData) {
this.diffModifyData = diffModifyData;
}
@Length(min=0, max=64, message="业务主键长度不能超过 64 个字符")
public String getBizKey() {
return bizKey;
@@ -220,6 +230,9 @@ public class Log extends DataEntity<Log> {
}
public String getExecuteTimeFormat(){
if (executeTime == null) {
executeTime = 0L;
}
return TimeUtils.formatDateAgo(executeTime);
}

View File

@@ -11,12 +11,12 @@ import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletRequest;
import io.netty.util.concurrent.DefaultThreadFactory;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.method.HandlerMethod;
@@ -29,6 +29,7 @@ import com.jeesite.common.mybatis.annotation.Column;
import com.jeesite.common.mybatis.annotation.Table;
import com.jeesite.common.mybatis.mapper.MapperHelper;
import com.jeesite.common.network.IpUtils;
import com.jeesite.common.utils.DiffDataUtils;
import com.jeesite.common.utils.SpringUtils;
import com.jeesite.common.web.http.UserAgentUtils;
import com.jeesite.modules.sys.entity.Log;
@@ -37,6 +38,7 @@ import com.jeesite.modules.sys.service.LogService;
import com.jeesite.modules.sys.service.MenuService;
import eu.bitwalker.useragentutils.UserAgent;
import io.netty.util.concurrent.DefaultThreadFactory;
/**
* 日志工具类
@@ -60,7 +62,7 @@ public class LogUtils {
// 参数名获取工具(尝试获取标注为@ModelAttribute注解的方法第一个参数名一般为主键名
private static ParameterNameDiscoverer pnd = new DefaultParameterNameDiscoverer();
/**
* 保存日志
*/
@@ -112,9 +114,14 @@ public class LogUtils {
if (throwable == null){
throwable = ExceptionUtils.getThrowable(request);
}
// 获取原数据和修改后的目标数据对象
Object sourceData = request.getAttribute(WebDataBinder.class.getName()+".SOURCE");
Object targetData = request.getAttribute(WebDataBinder.class.getName()+".TARGET");
// 异步保存日志
logThreadPool.submit(new SaveLogThread(log, handler, request.getContextPath(), throwable));
logThreadPool.submit(new SaveLogThread(log, handler, request.getContextPath(),
throwable, sourceData, targetData));
}
/**
* 保存日志线程
@@ -125,12 +132,17 @@ public class LogUtils {
private Object handler;
private String contextPath;
private Throwable throwable;
private Object sourceData;
private Object targetData;
public SaveLogThread(Log log, Object handler, String contextPath, Throwable throwable){
public SaveLogThread(Log log, Object handler, String contextPath,
Throwable throwable, Object sourceData, Object targetData){
this.log = log;
this.handler = handler;
this.contextPath = contextPath;
this.throwable = throwable;
this.sourceData = sourceData;
this.targetData = targetData;
}
@Override
@@ -217,11 +229,15 @@ public class LogUtils {
if (StringUtils.isBlank(log.getRequestUri()) && StringUtils.isBlank(log.getExceptionInfo())){
return;
}
// 如果是修改类型的日志,则获取修改前后的差异数据
if (Log.TYPE_UPDATE.equals(log.getLogType()) && sourceData != null && targetData != null) {
log.setDiffModifyData(DiffDataUtils.getDiffData(sourceData, targetData));
}
// 保存日志信息
log.setIsNewRecord(true);
Static.logService.insertLog(log);
}
}
}

View File

@@ -166,6 +166,12 @@
<#form:textarea path="exceptionInfo" rows="20" class="form-control autoHeight"/>
</div>
<% } %>
<% if(isNotBlank(log.diffModifyData)){ %>
<div class="form-unit">${text('差异修改数据')}</div>
<div class="ml10 mr10 mb10" style="word-break:break-all;">
${log.diffModifyData}
</div>
<% } %>
</div>
<div class="box-footer">
<div class="row">