excludePatterns = new ArrayList<>();
-
- /**
- * 是否记录请求参数(body/query/form)
- * 开启后会在日志中输出请求参数
- */
- private boolean isReqParams = false;
+ private boolean isPrintRequestParam = false;
/**
* 是否自动截断超长参数值(如 base64、大文本)
- * 开启后会对超过指定长度的参数值进行截断处理
+ * 开启后,超过指定长度的参数值将会自动截断处理
*/
- private boolean truncateLongParams = false;
+ private boolean longParamTruncate = false;
/**
* 超长参数检测阈值(单位:字符)
* 当参数值长度超过此值时,触发截断规则
- * 默认:2000
+ * 默认:2000,仅在 {@link #longParamTruncate} 启用时生效
*/
- private int ultraLongParamThreshold = 2000;
+ private int longParamThreshold = 2000;
/**
- * 超长参数最大展示长度(单位:字符)
- * 当参数超过ultraLongParamThreshold时,强制截断到此长度
- * 默认:50
+ * 超长参数最大保留长度(单位:字符)
+ * 当参数超过 {@link #longParamThreshold} 时,强制截断到此长度
+ * 默认:50,仅在 {@link #longParamTruncate} 启用时生效
*/
- private int ultraLongParamMaxLength = 50;
+ private int longParamMaxLength = 50;
/**
* 截断后追加的后缀符号(如配置 "..." 会让截断内容更直观)
- * 建议配置 3-5 个非占宽字符,默认为空不追加
+ * 建议配置 3-5 个非占宽字符,默认为 ...
+ * 仅在 {@link #longParamTruncate} 启用时生效
*/
- private String truncateSuffix = "...";
+ private String longParamSuffix = "...";
/**
* 是否过滤敏感参数
* 开启后会对敏感参数进行过滤,默认不过滤
*/
- private boolean isSensitiveParams = false;
+ private boolean isParamSensitive = false;
/**
* 敏感参数字段列表(如:password,token,idCard)
* 支持精确匹配(区分大小写)
* 示例值:password,oldPassword
*/
- private List sensitiveParamList = new ArrayList<>();
+ private List sensitiveParams = new ArrayList<>();
public boolean isPrint() {
return isPrint;
@@ -95,77 +90,59 @@ public class AccessLogProperties {
isPrint = print;
}
- public List getExcludePatterns() {
- return excludePatterns;
+ public boolean isPrintRequestParam() {
+ return isPrintRequestParam;
}
- public void setExcludePatterns(List excludePatterns) {
- this.excludePatterns = excludePatterns;
+ public void setPrintRequestParam(boolean printRequestParam) {
+ isPrintRequestParam = printRequestParam;
}
- public boolean isReqParams() {
- return isReqParams;
+ public boolean isLongParamTruncate() {
+ return longParamTruncate;
}
- public void setReqParams(boolean reqParams) {
- isReqParams = reqParams;
+ public void setLongParamTruncate(boolean longParamTruncate) {
+ this.longParamTruncate = longParamTruncate;
}
- public boolean isTruncateLongParams() {
- return truncateLongParams;
+ public int getLongParamThreshold() {
+ return longParamThreshold;
}
- public void setTruncateLongParams(boolean truncateLongParams) {
- this.truncateLongParams = truncateLongParams;
+ public void setLongParamThreshold(int longParamThreshold) {
+ this.longParamThreshold = longParamThreshold;
}
- public int getUltraLongParamThreshold() {
- return ultraLongParamThreshold;
+ public int getLongParamMaxLength() {
+ return longParamMaxLength;
}
- public void setUltraLongParamThreshold(int ultraLongParamThreshold) {
- this.ultraLongParamThreshold = ultraLongParamThreshold;
+ public void setLongParamMaxLength(int longParamMaxLength) {
+ this.longParamMaxLength = longParamMaxLength;
}
- public int getUltraLongParamMaxLength() {
- return ultraLongParamMaxLength;
+ public String getLongParamSuffix() {
+ return longParamSuffix;
}
- public void setUltraLongParamMaxLength(int ultraLongParamMaxLength) {
- this.ultraLongParamMaxLength = ultraLongParamMaxLength;
+ public void setLongParamSuffix(String longParamSuffix) {
+ this.longParamSuffix = longParamSuffix;
}
- public String getTruncateSuffix() {
- return truncateSuffix;
+ public boolean isParamSensitive() {
+ return isParamSensitive;
}
- public void setTruncateSuffix(String truncateSuffix) {
- this.truncateSuffix = truncateSuffix;
+ public void setParamSensitive(boolean paramSensitive) {
+ isParamSensitive = paramSensitive;
}
- public boolean isSensitiveParams() {
- return isSensitiveParams;
+ public List getSensitiveParams() {
+ return sensitiveParams;
}
- public void setSensitiveParams(boolean sensitiveParams) {
- isSensitiveParams = sensitiveParams;
- }
-
- public List getSensitiveParamList() {
- return sensitiveParamList;
- }
-
- public void setSensitiveParamList(List sensitiveParamList) {
- this.sensitiveParamList = sensitiveParamList;
- }
-
- /**
- * 是否匹配放行路由
- *
- * @param uri 请求 URI
- * @return 是否匹配
- */
- public boolean isMatch(String uri) {
- return this.getExcludePatterns().stream().anyMatch(pattern -> SpringWebUtils.isMatch(uri, pattern));
+ public void setSensitiveParams(List sensitiveParams) {
+ this.sensitiveParams = sensitiveParams;
}
}
diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java
index 911091ea..1ab72b26 100644
--- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java
+++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java
@@ -26,14 +26,14 @@ import java.util.List;
import java.util.Map;
/**
+ * 访问日志工具类
+ *
* @author echo
- * @since 2025/03/25 19:16
- **/
+ * @author Charles7c
+ * @since 2.10.0
+ */
public class AccessLogUtils {
- public AccessLogUtils() {
- }
-
/**
* 获取参数信息
*
@@ -42,8 +42,8 @@ public class AccessLogUtils {
* @return {@link String }
*/
public static String getParam(RecordableHttpRequest request, AccessLogProperties properties) {
- // 是否需要输出参数
- if (!properties.isReqParams()) {
+ // 是否需要打印请求参数
+ if (!properties.isPrintRequestParam()) {
return null;
}
@@ -54,16 +54,16 @@ public class AccessLogUtils {
}
// 是否需要对特定入参脱敏
- if (properties.isSensitiveParams()) {
- params = filterSensitiveParams(params, properties.getSensitiveParamList());
+ if (properties.isParamSensitive()) {
+ params = filterSensitiveParams(params, properties.getSensitiveParams());
}
// 是否自动截断超长参数值
- if (properties.isTruncateLongParams()) {
- params = truncateLongParams(params, properties.getUltraLongParamThreshold(), properties
- .getUltraLongParamMaxLength(), properties.getTruncateSuffix());
+ if (properties.isLongParamTruncate()) {
+ params = truncateLongParams(params, properties.getLongParamThreshold(), properties
+ .getLongParamMaxLength(), properties.getLongParamSuffix());
}
- return "param:" + JSONUtil.toJsonStr(params);
+ return JSONUtil.toJsonStr(params);
}
/**
@@ -80,9 +80,7 @@ public class AccessLogUtils {
Map filteredParams = new HashMap<>(params);
for (String sensitiveKey : sensitiveParams) {
- if (filteredParams.containsKey(sensitiveKey)) {
- filteredParams.put(sensitiveKey, "***");
- }
+ filteredParams.computeIfPresent(sensitiveKey, (key, value) -> "***");
}
return filteredParams;
}
@@ -115,4 +113,7 @@ public class AccessLogUtils {
}
return truncatedParams;
}
+
+ private AccessLogUtils() {
+ }
}
diff --git a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java
index 4dcf2fc0..c1f0889d 100644
--- a/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java
+++ b/continew-starter-log/continew-starter-log-interceptor/src/main/java/top/continew/starter/log/interceptor/LogInterceptor.java
@@ -64,7 +64,7 @@ public class LogInterceptor implements HandlerInterceptor {
@NonNull HttpServletResponse response,
@NonNull Object handler) {
Instant startTime = Instant.now();
- logHandler.processAccessLogStartReq(AccessLogContext.builder()
+ logHandler.accessLogStart(AccessLogContext.builder()
.startTime(startTime)
.request(new RecordableServletHttpRequest(request))
.properties(logProperties)
@@ -84,7 +84,7 @@ public class LogInterceptor implements HandlerInterceptor {
Exception e) {
try {
Instant endTime = Instant.now();
- logHandler.processAccessLogEndReq(AccessLogContext.builder()
+ logHandler.accessLogFinish(AccessLogContext.builder()
.endTime(endTime)
.response(new RecordableServletHttpResponse(response, response.getStatus()))
.build());