test
This commit is contained in:
+1
-54
@@ -244,63 +244,10 @@ class LoggerIns
|
||||
// 3. 添加名称前缀 [name]
|
||||
$namePrefix = "<RP_NAME:{$this->name}>";
|
||||
$result = $namePrefix . $processedMessage;
|
||||
if ($this->isFilter()) return "";
|
||||
// if ($this->isFilter()) return "";
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前日志是否需要过滤(核心过滤逻辑)
|
||||
* @return bool true=需要过滤,false=保留日志
|
||||
*/
|
||||
private function isFilter(): bool
|
||||
{
|
||||
$logFilter = Config::getInstance()->logFilter;
|
||||
// 无过滤规则时直接返回false(不过滤)
|
||||
if (empty($logFilter)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 1. 解析调用堆栈,获取业务代码的类名/方法名
|
||||
$stackInfo = $this->parseBusinessStackInfo();
|
||||
$className = $stackInfo['class'] ?? $this->name ?? '';
|
||||
$methodName = $stackInfo['method'] ?? '';
|
||||
|
||||
// 2. 通配符匹配函数
|
||||
$matchWildcard = function (string $pattern, string $value): bool {
|
||||
// 空规则特殊处理:如:debug 拆分后类规则为空,代表匹配所有类
|
||||
if ($pattern === '') {
|
||||
return true;
|
||||
}
|
||||
// 将通配符*转换为正则的.*,并转义其他特殊字符
|
||||
$regexPattern = preg_quote($pattern, '/');
|
||||
$regexPattern = str_replace('\*', '.*', $regexPattern);
|
||||
// 正则全程匹配
|
||||
return preg_match('/^' . $regexPattern . '$/', $value) === 1;
|
||||
};
|
||||
|
||||
// 3. 遍历过滤规则,匹配则返回true(需要过滤)
|
||||
foreach ($logFilter as $filter) {
|
||||
// 跳过非法过滤规则
|
||||
if (!is_string($filter)) {
|
||||
continue;
|
||||
}
|
||||
// 拆分规则为类规则和方法规则(最多拆2部分,避免方法名含:)
|
||||
[$filterClassRule, $filterMethodRule] = array_pad(explode(':', $filter, 2), 2, '*');
|
||||
|
||||
// 匹配类名规则(支持通配符*)
|
||||
$isClassMatch = $matchWildcard($filterClassRule, $className);
|
||||
// 匹配方法名规则(支持通配符*)
|
||||
$isMethodMatch = $matchWildcard($filterMethodRule, $methodName);
|
||||
|
||||
// 类和方法都匹配时,返回true(过滤该日志)
|
||||
if ($isClassMatch && $isMethodMatch) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// 无匹配规则,返回false(保留日志)
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析调用堆栈,获取实际业务代码的类名和方法名
|
||||
|
||||
Reference in New Issue
Block a user