修改日志结构

This commit is contained in:
xinzhu.yin
2026-04-29 16:43:31 +08:00
parent 377bba2a0b
commit afd83448ed
6 changed files with 216 additions and 46 deletions

View File

@@ -1,8 +1,22 @@
import logging
import threading
from datetime import datetime
import tkinter as tk
import ttkbootstrap as ttk
# 与 app.logging_setup 共享的映射;放在这里避免循环 import
_GUI_LEVEL_TO_LOG = {
"debug": logging.DEBUG,
"info": logging.INFO,
"success": logging.INFO,
"warning": logging.WARNING,
"error": logging.ERROR,
"separator": logging.INFO,
"blank": logging.DEBUG,
}
class PQLogGUI(ttk.Frame):
VALID_LEVELS = {"info", "success", "warning", "error", "debug", "separator", "blank"}
@@ -10,6 +24,8 @@ class PQLogGUI(ttk.Frame):
super().__init__(parent)
self._line_count = 0
self._max_lines = 1500
# 与 stdlib logging 联通的 logger由 logging_setup 配置 file handler
self._logger = logging.getLogger("pq.gui")
self.create_widgets()
def create_widgets(self):
@@ -62,13 +78,25 @@ class PQLogGUI(ttk.Frame):
self._configure_tags()
self.log_text.config(state=tk.DISABLED)
def log(self, message, level="info"):
def log(self, message, level="info", _from_logging=False):
if threading.current_thread() is not threading.main_thread():
self.after(0, self.log, message, level)
self.after(0, self.log, message, level, _from_logging)
return
text = "" if message is None else str(message)
normalized_level = self._normalize_level(level, text)
# 转发到 stdlib logging落到文件_from_logging=True 表示来源已是
# logging不再回写避免重复。带 _from_gui 标记TkLogHandler 会跳过。
if not _from_logging and normalized_level != "blank":
log_level = _GUI_LEVEL_TO_LOG.get(normalized_level, logging.INFO)
try:
self._logger.log(
log_level, text, extra={"_from_gui": True}
)
except Exception:
pass
self.log_text.config(state=tk.NORMAL)
self._append_message(text, normalized_level)
self.log_text.see(tk.END)