modify: 日志重复问题

This commit is contained in:
2026-08-25 09:29:27 +08:00
parent eeffc06841
commit 4d6b759a1b
8 changed files with 317 additions and 24 deletions
+27 -4
View File
@@ -17,14 +17,16 @@ import tempfile
import time
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent / "src"))
sys.path.insert(0, str(Path(__file__).resolve().parent / "utils" / "worker" / "src"))
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src"))
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "utils" / "worker" / "src"))
_tmp = tempfile.TemporaryDirectory()
# 预加载真实 logger 模块(不触发 chongming 包链导入),把日志目录重定向到
# 临时目录,之后 import chongming.app 时 get_logger 即用新路径
_logger_path = Path(__file__).resolve().parent / "src" / "chongming" / "app" / "core" / "logger.py"
_logger_path = (
Path(__file__).resolve().parent.parent / "src" / "chongming" / "app" / "core" / "logger.py"
)
_logger_spec = importlib.util.spec_from_file_location("chongming.app.core.logger", _logger_path)
logger_mod = importlib.util.module_from_spec(_logger_spec)
sys.modules["chongming.app.core.logger"] = logger_mod
@@ -103,7 +105,27 @@ def test_queue_full_drops():
logger_mod._log_queue = old
# ---- 4. 端到端:中间件采样 + 异步落盘 ----
# ---- 4. 按名隔离:不同 log_name 的记录不串文件 ----
def test_name_isolation():
logger_mod.get_logger("test_b")
lg_a = logger_mod.get_logger("test_async")
lg_b = logger_mod.get_logger("test_b")
lg_a.info("隔离测试 A 专属 %d", 1)
lg_b.info("隔离测试 B 专属 %d", 2)
ok = _wait_for(
lambda: any(
"隔离测试 B 专属 2" in f.read_text(errors="ignore")
for f in Path(_tmp.name).glob("test_b-*.log")
)
)
check("test_b 记录写入 test_b 文件", ok)
a_text = "".join(f.read_text(errors="ignore") for f in Path(_tmp.name).glob("test_async-*.log"))
b_text = "".join(f.read_text(errors="ignore") for f in Path(_tmp.name).glob("test_b-*.log"))
check("test_async 记录不串入 test_b 文件", "隔离测试 A 专属" not in b_text)
check("test_b 记录不串入 test_async 文件", "隔离测试 B 专属" not in a_text)
# ---- 5. 端到端:中间件采样 + 异步落盘 ----
def test_e2e_middleware():
import asyncio
import httpx
@@ -130,6 +152,7 @@ def test_e2e_middleware():
test_sampling()
test_async_queue_write()
test_queue_full_drops()
test_name_isolation()
test_e2e_middleware()
# 关闭监听线程并释放文件句柄,再清理临时目录