优化日志保存机制,添加时间节点获取
This commit is contained in:
@@ -14,10 +14,14 @@ import org.springframework.web.util.ContentCachingResponseWrapper;
|
||||
import xtools.app.common.log.whitelist.HttpLogWhitelist;
|
||||
import xtools.base.config.BaseParams;
|
||||
import xtools.boot.api.constant.BootCommonConstant;
|
||||
import xtools.boot.api.model.dto.log.LogTrack;
|
||||
import xtools.boot.core.utils.PathPatternUtils;
|
||||
import xtools.boot.core.utils.SpringContextUtils;
|
||||
import xtools.boot.log.LogBus;
|
||||
import xtools.boot.log.enums.LogBusBaseType;
|
||||
import xtools.boot.log.holder.LogTrackHolder;
|
||||
import xtools.boot.log.interfaces.LogBusType;
|
||||
import xtools.boot.thread.utils.VirtualThreadTaskUtils;
|
||||
import xtools.boot.web.filter.base.BaseFilter;
|
||||
import xtools.core.CollectionUtils;
|
||||
import xtools.core.enums.LogLevel;
|
||||
@@ -94,18 +98,33 @@ public class HttpLogFilter extends BaseFilter implements Ordered, BaseParams {
|
||||
|
||||
// 获取请求日志
|
||||
JSONObject log = getRequestLog(request);
|
||||
|
||||
long t1 = System.currentTimeMillis() - startTime;
|
||||
|
||||
// 记录请求日志
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.HTTP_REQUEST).data(log).save();
|
||||
saveLog(LogBusBaseType.HTTP_REQUEST, log);
|
||||
|
||||
long t2 = System.currentTimeMillis() - startTime;
|
||||
|
||||
// 包装请求和响应
|
||||
ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request, DEFAULT_CACHE_LIMIT);
|
||||
|
||||
long t3 = System.currentTimeMillis() - startTime;
|
||||
|
||||
ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
|
||||
|
||||
long t4 = System.currentTimeMillis() - startTime;
|
||||
|
||||
// 处理请求
|
||||
filterChain.doFilter(requestWrapper, responseWrapper);
|
||||
|
||||
long t5 = System.currentTimeMillis() - startTime;
|
||||
|
||||
// 获取请求参数
|
||||
Object reqParam = getContent(requestWrapper.getContentAsByteArray());
|
||||
|
||||
long t6 = System.currentTimeMillis() - startTime;
|
||||
|
||||
Object respData;
|
||||
if (!PathPatternUtils.match(RESP_WHITE_LIST, uri)) {
|
||||
// 获取响应内容
|
||||
@@ -113,14 +132,33 @@ public class HttpLogFilter extends BaseFilter implements Ordered, BaseParams {
|
||||
} else {
|
||||
respData = "ignore";
|
||||
}
|
||||
|
||||
long t7 = System.currentTimeMillis() - startTime;
|
||||
|
||||
// 将内容复制回原始响应
|
||||
responseWrapper.copyBodyToResponse();
|
||||
|
||||
long t8 = System.currentTimeMillis() - startTime;
|
||||
|
||||
// 保存日志
|
||||
log.put("reqBody", reqParam);
|
||||
log.put("respData", respData);
|
||||
log.put("execTime", System.currentTimeMillis() - startTime);
|
||||
|
||||
long t9 = System.currentTimeMillis() - startTime;
|
||||
|
||||
log.put("t1", t1);
|
||||
log.put("t2", t2);
|
||||
log.put("t3", t3);
|
||||
log.put("t4", t4);
|
||||
log.put("t5", t5);
|
||||
log.put("t6", t6);
|
||||
log.put("t7", t7);
|
||||
log.put("t8", t8);
|
||||
log.put("t9", t9);
|
||||
|
||||
// 记录响应日志
|
||||
LogBus.init(LogLevel.INFO, LogBusBaseType.HTTP_REQUEST).data(log).save();
|
||||
saveLog(LogBusBaseType.HTTP_RESPONSE, log);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -154,7 +192,7 @@ public class HttpLogFilter extends BaseFilter implements Ordered, BaseParams {
|
||||
* @param request 请求
|
||||
* @return 请求日志
|
||||
*/
|
||||
public JSONObject getRequestLog(HttpServletRequest request) {
|
||||
private JSONObject getRequestLog(HttpServletRequest request) {
|
||||
// 基础日志信息
|
||||
JSONObject log = new JSONObject();
|
||||
log.put("uri", HeaderUtils.getUri(request));
|
||||
@@ -169,4 +207,15 @@ public class HttpLogFilter extends BaseFilter implements Ordered, BaseParams {
|
||||
return log;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存日志
|
||||
*
|
||||
* @param type 日志类型
|
||||
* @param log 日志
|
||||
*/
|
||||
private void saveLog(LogBusType type, Object log) {
|
||||
LogTrack track = LogTrackHolder.get();
|
||||
VirtualThreadTaskUtils.simple(() -> LogBus.init(LogLevel.INFO, type, track).data(log).save());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user