From 71a9f4a25f044deb865a9d84fd785a6105c886f0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 3 Jun 2026 12:15:47 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=AE=80=E7=AE=80=E5=8D=95=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../douyin_logging_wrapper_pkg/enhanced.py | 4 + .../douyin_logging_wrapper_pkg/simple.py | 70 ++++++++++++- tests/test_logging_simple_mode.py | 98 +++++++++++++++++++ 3 files changed, 169 insertions(+), 3 deletions(-) create mode 100644 tests/test_logging_simple_mode.py diff --git a/douyin/logging/douyin_logging_wrapper_pkg/enhanced.py b/douyin/logging/douyin_logging_wrapper_pkg/enhanced.py index c25bc61..80fb4ff 100644 --- a/douyin/logging/douyin_logging_wrapper_pkg/enhanced.py +++ b/douyin/logging/douyin_logging_wrapper_pkg/enhanced.py @@ -303,6 +303,9 @@ def enhanced_log_kv(analyser: Any, *args: Any, **fields: Any) -> None: if important_fields: log_parts.append(" ".join(important_fields)) + # simple 模式需要这个类型来决定是否静默 DEBUG/PERF/SCREENSHOT。 + fields["_log_type"] = log_type + # 构建完整日志行 log_line = " ".join(log_parts) @@ -350,6 +353,7 @@ def enhanced_log_kv(analyser: Any, *args: Any, **fields: Any) -> None: # 根据模式决定日志格式 if log_format_mode == "detailed": # 详细模式:保留之前的复杂格式 + fields.pop("_log_type", None) return _enhanced_log_kv_detailed( analyser, tag, diff --git a/douyin/logging/douyin_logging_wrapper_pkg/simple.py b/douyin/logging/douyin_logging_wrapper_pkg/simple.py index 99be083..d30b853 100644 --- a/douyin/logging/douyin_logging_wrapper_pkg/simple.py +++ b/douyin/logging/douyin_logging_wrapper_pkg/simple.py @@ -21,6 +21,68 @@ from ._common import ( _performance_tracker, ) +_SIMPLE_NOISE_LOG_TYPES = {"DEBUG", "PERF", "SCREENSHOT", "FLOW"} +_SIMPLE_NOISE_TAGS = { + "性能", + "流程标志", + "调试图像", + "步骤耗时", + "[性能]", + "[流程标志]", + "[调试图像]", + "[步骤耗时]", +} +_SIMPLE_NOISE_PATTERNS = ( + "[FLOW_FLAG]", + "OCR调试(", + "result[0] keys=", + "dt_polys", + "rec_texts", + "调试图像", + "综合标注图已保存", + "像素阶段总览保存", +) +_SIMPLE_MAX_LINE_LEN = 220 + + +def _clean_simple_tag(tag: str) -> str: + return str(tag or "").strip().strip("[]").strip() + + +def _should_skip_simple_stdout( + tag: str, + msg: str, + log_level: str, + fields: Dict[str, Any], + log_line: str, +) -> bool: + """Simple 模式只显示业务状态;调试/性能细节留给 detailed/debug。""" + if fields.get("exc_info") is not None: + return False + + if str(log_level or "").upper() in {"WARNING", "ERROR", "CRITICAL"}: + return False + + tag_clean = _clean_simple_tag(tag) + if tag_clean in _SIMPLE_NOISE_TAGS: + return True + + log_type = str(fields.get("_log_type", "") or "").upper() + if log_type in _SIMPLE_NOISE_LOG_TYPES: + return True + + text = f"{tag} {msg} {log_line}" + if any(pattern in text for pattern in _SIMPLE_NOISE_PATTERNS): + return True + + return False + + +def _compact_simple_line(log_line: str) -> str: + if len(log_line) <= _SIMPLE_MAX_LINE_LEN: + return log_line + return log_line[: _SIMPLE_MAX_LINE_LEN - 3].rstrip() + "..." + def _enhanced_log_kv_simple( analyser: Any, @@ -35,6 +97,11 @@ def _enhanced_log_kv_simple( color: Optional[str] = None, ) -> None: """简洁模式的日志记录""" + # 构建简洁的日志行 + log_line = _compact_simple_line(" ".join(log_parts)) + if _should_skip_simple_stdout(tag, msg, log_level, fields, log_line): + return + # 输出分隔符(如果需要)- 使用更简洁的分隔符 if need_separator: try: @@ -46,9 +113,6 @@ def _enhanced_log_kv_simple( # 更新上一个日志标签 analyser._last_log_tag = tag - # 构建简洁的日志行 - log_line = " ".join(log_parts) - # 应用颜色(如果指定了颜色) if color: try: diff --git a/tests/test_logging_simple_mode.py b/tests/test_logging_simple_mode.py new file mode 100644 index 0000000..4fcc16e --- /dev/null +++ b/tests/test_logging_simple_mode.py @@ -0,0 +1,98 @@ +from __future__ import annotations + +from types import SimpleNamespace + +from douyin.logging.douyin_logging_wrapper_pkg.enhanced import enhanced_log_kv +from douyin.logging.douyin_logging_wrapper_pkg.simple import _enhanced_log_kv_simple + + +def _analyser() -> SimpleNamespace: + return SimpleNamespace(_last_log_tag=None, _log_seq=0) + + +def test_simple_suppresses_debug_perf_and_screenshot_noise(capsys): + analyser = _analyser() + + _enhanced_log_kv_simple( + analyser, + "性能", + "福袋详情关键耗时(ms)", + "INFO", + ["[2026-06-03 11:09:05.450]", "[INFO]", "[性能]", "福袋详情关键耗时(ms)"], + "2026-06-03 11:09:05.450", + {"_log_type": "PERF", "total_ms": 123.4}, + False, + "", + ) + _enhanced_log_kv_simple( + analyser, + "调试图像", + "福袋详情综合标注图已保存", + "INFO", + ["[2026-06-03 11:09:08.342]", "[INFO]", "[调试图像]", "福袋详情综合标注图已保存"], + "2026-06-03 11:09:08.342", + {"_log_type": "SCREENSHOT"}, + False, + "", + ) + + assert capsys.readouterr().out == "" + + +def test_simple_suppresses_flow_and_ocr_raw_debug(capsys): + analyser = _analyser() + + _enhanced_log_kv_simple( + analyser, + "流程标志", + "[FLOW_FLAG] fudai_contain:start", + "INFO", + ["[2026-06-03 11:09:05.462]", "[INFO]", "[流程标志]", "[FLOW_FLAG] fudai_contain:start"], + "2026-06-03 11:09:05.462", + {"_log_type": "OP"}, + False, + "", + ) + _enhanced_log_kv_simple( + analyser, + "OCR", + "OCR调试(福袋内容): result[0] keys=['input_path', 'dt_polys', 'rec_texts']", + "INFO", + [ + "[2026-06-03 11:09:28.000]", + "[INFO]", + "[OCR]", + "OCR调试(福袋内容): result[0] keys=['input_path', 'dt_polys', 'rec_texts']", + ], + "2026-06-03 11:09:28.000", + {"_log_type": "DEBUG"}, + False, + "", + ) + + assert capsys.readouterr().out == "" + + +def test_simple_keeps_business_summary_and_warnings(capsys): + analyser = _analyser() + + enhanced_log_kv( + analyser, + "福袋详情", + "read_fudai_detail_once完成(成功)", + elapsed_seconds=1.2, + value_yuan=198, + ) + enhanced_log_kv( + analyser, + "验证", + "倒计时OCR结果完全为空", + level="WARNING", + log_type="DEBUG", + ) + + out = capsys.readouterr().out + assert "[福袋详情]" in out + assert "read_fudai_detail_once完成(成功)" in out + assert "[WARNING]" in out + assert "倒计时OCR结果完全为空" in out