perf(log): 访问日志过滤资源路径

This commit is contained in:
吴泽威
2025-04-01 15:34:54 +08:00
parent 5822d073fb
commit a6a44cd461
2 changed files with 24 additions and 2 deletions

View File

@@ -174,8 +174,9 @@ public abstract class AbstractLogHandler implements LogHandler {
public void accessLogStart(AccessLogContext accessLogContext) {
AccessLogProperties properties = accessLogContext.getProperties().getAccessLog();
// 是否需要打印 规则: 是否打印开关 或 放行路径
if (!properties.isEnabled() || accessLogContext.getProperties()
.isMatch(accessLogContext.getRequest().getPath())) {
if (!properties.isEnabled() || AccessLogUtils.exclusionPath(accessLogContext.getProperties(), accessLogContext
.getRequest()
.getPath())) {
return;
}
// 构建上下文

View File

@@ -20,6 +20,8 @@ import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import top.continew.starter.log.http.RecordableHttpRequest;
import top.continew.starter.log.model.AccessLogProperties;
import top.continew.starter.log.model.LogProperties;
import top.continew.starter.web.util.SpringWebUtils;
import java.util.HashMap;
import java.util.List;
@@ -34,6 +36,12 @@ import java.util.Map;
*/
public class AccessLogUtils {
/**
* 资源路径 - doc 路径
*/
private static final List<String> RESOURCE_PATH = List
.of("/doc/**", "/v2/api-docs/**", "/v3/api-docs/**", "/webjars/**", "/swagger-resources/**", "/swagger-ui.html");
/**
* 获取参数信息
*
@@ -72,6 +80,19 @@ public class AccessLogUtils {
return JSONUtil.toJsonStr(params);
}
/**
* 排除路径
*
* @param properties 属性
* @param path 路径
* @return boolean
*/
public static boolean exclusionPath(LogProperties properties, String path) {
// 放行路由配置的排除检查
return properties.isMatch(path) || RESOURCE_PATH.stream()
.anyMatch(resourcePath -> SpringWebUtils.isMatch(path, resourcePath));
}
/**
* 过滤敏感参数
*