refactor: 优化部分代码及方法命名,移除 SpringWebUtils 部分重复方法

This commit is contained in:
2025-04-01 22:08:54 +08:00
parent 7e8a15ae8a
commit f2ba10beae
9 changed files with 171 additions and 178 deletions

View File

@@ -166,14 +166,14 @@ public abstract class AbstractLogHandler implements LogHandler {
AccessLogProperties properties = accessLogContext.getProperties().getAccessLog();
// 是否需要打印 规则: 是否打印开关 或 放行路径
if (!properties.isEnabled() || AccessLogUtils.exclusionPath(accessLogContext.getProperties(), ServletUtils
.getReqPath())) {
.getRequestPath())) {
return;
}
// 构建上下文
logContextThread.set(accessLogContext);
String param = AccessLogUtils.getParam(properties);
log.info(param != null ? "[Start] [{}] {} param: {}" : "[Start] [{}] {}", ServletUtils
.getReqMethod(), ServletUtils.getReqPath(), param);
.getRequestMethod(), ServletUtils.getRequestPath(), param);
}
@Override
@@ -184,8 +184,8 @@ public abstract class AbstractLogHandler implements LogHandler {
}
try {
Duration timeTaken = Duration.between(logContext.getStartTime(), accessLogContext.getEndTime());
log.info("[End] [{}] {} {} {}ms", ServletUtils.getReqMethod(), ServletUtils.getReqPath(), ServletUtils
.getRespStatus(), timeTaken.toMillis());
log.info("[End] [{}] {} {} {}ms", ServletUtils.getRequestMethod(), ServletUtils
.getRequestPath(), ServletUtils.getResponseStatus(), timeTaken.toMillis());
} finally {
logContextThread.remove();
}

View File

@@ -81,14 +81,14 @@ public class LogRequest {
private String os;
public LogRequest(Set<Include> includes) {
this.method = ServletUtils.getReqMethod();
this.url = ServletUtils.getReqUrl();
this.ip = ServletUtils.getReqIp();
this.headers = (includes.contains(Include.REQUEST_HEADERS)) ? ServletUtils.getReqHeaders() : null;
this.method = ServletUtils.getRequestMethod();
this.url = ServletUtils.getRequestUrl();
this.ip = ServletUtils.getRequestIp();
this.headers = (includes.contains(Include.REQUEST_HEADERS)) ? ServletUtils.getRequestHeaders() : null;
if (includes.contains(Include.REQUEST_BODY)) {
this.body = ServletUtils.getReqBody();
this.body = ServletUtils.getRequestBody();
} else if (includes.contains(Include.REQUEST_PARAM)) {
this.param = ServletUtils.getReqParam();
this.param = ServletUtils.getRequestParams();
}
this.address = (includes.contains(Include.IP_ADDRESS))
? ExceptionUtils.exToNull(() -> IpUtils.getIpv4Address(this.ip))

View File

@@ -51,12 +51,12 @@ public class LogResponse {
private Map<String, Object> param;
public LogResponse(Set<Include> includes) {
this.status = ServletUtils.getRespStatus();
this.headers = (includes.contains(Include.RESPONSE_HEADERS)) ? ServletUtils.getRespHeaders() : null;
this.status = ServletUtils.getResponseStatus();
this.headers = (includes.contains(Include.RESPONSE_HEADERS)) ? ServletUtils.getResponseHeaders() : null;
if (includes.contains(Include.RESPONSE_BODY)) {
this.body = ServletUtils.getRespBody();
this.body = ServletUtils.getResponseBody();
} else if (includes.contains(Include.RESPONSE_PARAM)) {
this.param = ServletUtils.getRespParam();
this.param = ServletUtils.getResponseParams();
}
}

View File

@@ -16,16 +16,16 @@
package top.continew.starter.log.util;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.ObjectUtil;
import top.continew.starter.json.jackson.util.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import top.continew.starter.json.jackson.util.JSONUtils;
import top.continew.starter.log.model.AccessLogProperties;
import top.continew.starter.log.model.LogProperties;
import top.continew.starter.web.util.ServletUtils;
import top.continew.starter.web.util.SpringWebUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
/**
@@ -37,6 +37,9 @@ import java.util.stream.Collectors;
*/
public class AccessLogUtils {
private AccessLogUtils() {
}
/**
* 资源路径 - doc 路径
*/
@@ -58,7 +61,7 @@ public class AccessLogUtils {
// 参数为空返回空
Object params;
try {
params = ServletUtils.getAccessLogReqParam();
params = getAccessLogReqParam();
} catch (Exception e) {
return null;
}
@@ -76,7 +79,7 @@ public class AccessLogUtils {
params = processTruncateLongParams(params, properties.getLongParamThreshold(), properties
.getLongParamMaxLength(), properties.getLongParamSuffix());
}
return JSONUtil.toJsonStr(params);
return JSONUtils.toJsonStr(params);
}
/**
@@ -146,7 +149,7 @@ public class AccessLogUtils {
return truncateLongParams((Map<String, Object>)params, threshold, maxLength, suffix);
} else if (params instanceof List) {
return ((List<?>)params).stream()
.filter(item -> item instanceof Map)
.filter(Map.class::isInstance)
.map(item -> truncateLongParams((Map<String, Object>)item, threshold, maxLength, suffix))
.collect(Collectors.toList());
}
@@ -182,6 +185,25 @@ public class AccessLogUtils {
return truncatedParams;
}
private AccessLogUtils() {
/**
* 获取访问日志请求参数
*
* @return {@link Object }
*/
private static Object getAccessLogReqParam() {
String body = ServletUtils.getRequestBody();
if (CharSequenceUtil.isNotBlank(body) && JSONUtils.isTypeJSON(body)) {
try {
JsonNode jsonNode = JSONUtils.getObjectMapper().readTree(body);
if (jsonNode.isArray()) {
return JSONUtils.toBean(body, List.class);
} else {
return JSONUtils.toBean(body, Map.class);
}
} catch (Exception e) {
return null;
}
}
return Collections.unmodifiableMap(ServletUtils.getRequestParams());
}
}