83 lines
3.3 KiB
Python
83 lines
3.3 KiB
Python
"""日志配置 + GUI 主题保存逻辑。
|
|
|
|
从 ``gui/gui_app.py`` 抽出。原本是 ``main()`` 内的闭包 ``save_logging_config``,
|
|
读取 18 个 tkinter Var 写入 config.json。
|
|
|
|
依赖通过显式参数注入:
|
|
- ``vars_`` - 一个名→Var 的 ``Dict[str, tk.Variable]``,键名与原 main() 中的变量名一致
|
|
- ``read_config`` - 读配置回调
|
|
- ``write_config`` - 写配置回调
|
|
- ``on_line`` - 日志回调
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import tkinter as tk
|
|
from typing import Callable, Dict
|
|
|
|
|
|
def save_logging_config(
|
|
*,
|
|
vars_: Dict[str, tk.Variable],
|
|
read_config: Callable[[], dict],
|
|
write_config: Callable[[dict], None],
|
|
on_line: Callable[[str], None],
|
|
) -> None:
|
|
"""保存日志配置到配置文件"""
|
|
try:
|
|
# 读取当前配置
|
|
cfg = read_config()
|
|
|
|
# 更新日志相关配置
|
|
if "logging" not in cfg:
|
|
cfg["logging"] = {}
|
|
|
|
cfg["logging"]["log_format_mode"] = vars_["logging_format_mode_var"].get()
|
|
cfg["logging"]["beautify_output"] = bool(vars_["logging_beautify_var"].get())
|
|
cfg["logging"]["color_output"] = bool(vars_["logging_color_var"].get())
|
|
cfg["logging"]["show_timestamps"] = bool(vars_["logging_show_timestamps_var"].get())
|
|
cfg["logging"]["use_dated_logs"] = bool(vars_["logging_use_dated_logs_var"].get())
|
|
cfg["logging"]["debug_detail_level"] = vars_["logging_debug_level_var"].get()
|
|
cfg["logging"]["error_detail_level"] = vars_["logging_error_level_var"].get()
|
|
cfg["logging"]["action_log_format"] = vars_["logging_action_format_var"].get()
|
|
cfg["logging"]["enable_monitor"] = bool(vars_["logging_enable_monitor_var"].get())
|
|
cfg["logging"]["keep_days"] = int(vars_["logging_keep_days_var"].get())
|
|
cfg["logging"]["compress_logs"] = bool(vars_["logging_compress_logs_var"].get())
|
|
cfg["logging"]["max_total_size_gb"] = float(
|
|
vars_["logging_max_total_size_gb_var"].get()
|
|
)
|
|
cfg["logging"]["compress_after_days"] = int(
|
|
vars_["logging_compress_after_days_var"].get()
|
|
)
|
|
cfg["logging"]["monitor_warning_mb"] = float(
|
|
vars_["logging_monitor_warning_mb_var"].get()
|
|
)
|
|
cfg["logging"]["monitor_critical_mb"] = float(
|
|
vars_["logging_monitor_critical_mb_var"].get()
|
|
)
|
|
cfg["logging"]["performance_tracking"] = {
|
|
"enabled": bool(vars_["logging_performance_enabled_var"].get()),
|
|
"track_function_calls": bool(vars_["logging_track_functions_var"].get()),
|
|
}
|
|
cfg["log_unified_enabled"] = bool(vars_["log_unified_enabled_var"].get())
|
|
cfg["log_unified_path"] = str(vars_["log_unified_path_var"].get() or "logs/unified.log")
|
|
|
|
# 更新GUI主题
|
|
if "gui" not in cfg:
|
|
cfg["gui"] = {}
|
|
cfg["gui"]["theme"] = vars_["gui_theme_var"].get()
|
|
cfg["gui"]["realtime_status"] = {
|
|
"refresh_interval_ms": int(
|
|
vars_["gui_realtime_refresh_interval_ms_var"].get() or 1000
|
|
),
|
|
"tail_items": int(vars_["gui_realtime_tail_items_var"].get() or 50),
|
|
}
|
|
|
|
# 保存配置
|
|
write_config(cfg)
|
|
|
|
on_line("[配置] 日志配置已保存")
|
|
except Exception as e:
|
|
on_line(f"[配置] 保存日志配置失败:{e}")
|
|
|