Files
douyin_guaji-main/douyin_logger.py
T
Your Name 2cd81c05a3 fix: Adjust configuration settings and improve error handling
- Updated `config.json` to modify `yanghao_recommend_interval_seconds` for better timing control.
- Refactored logging in `douyin_detection.py` to enhance readability and error handling.
- Improved pixel loading and error management in image processing functions across multiple modules.
- Enhanced GUI elements in `gui_app.py` to support new configuration options and improve user experience.
2025-12-21 21:55:30 +08:00

300 lines
9.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
增强的日志系统配置模块
====================
功能:
1. 标准 Python logging 模块集成
2. 日志级别管理(DEBUG, INFO, WARNING, ERROR, CRITICAL)
3. 日志轮转(按大小和时间)
4. 清晰的日志格式(时间戳、级别、模块、函数、行号)
5. 异常堆栈完整记录
6. 根据环境动态调整日志级别
"""
from __future__ import annotations
import logging
import logging.handlers
import os
import sys
import json
from typing import Optional, Dict, Any
from datetime import datetime
import path_config
# 日志配置默认值
DEFAULT_LOG_LEVEL = "INFO"
DEFAULT_LOG_FILE = "logs/unified.log"
DEFAULT_MAX_BYTES = 10 * 1024 * 1024 # 10MB
DEFAULT_BACKUP_COUNT = 5
DEFAULT_WHEN = "midnight" # 每天午夜轮转
DEFAULT_INTERVAL = 1 # 每1天轮转一次
def get_log_config() -> Dict[str, Any]:
"""从 config.json 读取日志配置"""
try:
config_path = path_config.get_config_path()
if os.path.exists(config_path):
with open(config_path, "r", encoding="utf-8") as f:
config = json.load(f)
return config.get("logging", {})
except Exception:
pass
return {}
def get_log_level() -> str:
"""获取日志级别(从配置或环境变量)"""
# 优先检查环境变量(开发调试时使用)
env_level = os.environ.get("DOUYIN_LOG_LEVEL", "").upper()
if env_level in ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"):
return env_level
# 从配置文件读取
log_config = get_log_config()
level = log_config.get("level", DEFAULT_LOG_LEVEL).upper()
if level in ("DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"):
return level
return DEFAULT_LOG_LEVEL
def get_log_file_path() -> str:
"""获取日志文件路径"""
log_config = get_log_config()
path = log_config.get("file_path", DEFAULT_LOG_FILE)
if not os.path.isabs(path):
path = os.path.join(path_config.PROJECT_ROOT, path)
return path
def get_rotation_config() -> Dict[str, Any]:
"""获取日志轮转配置"""
log_config = get_log_config()
rotation = log_config.get("rotation", {})
return {
"enabled": rotation.get("enabled", True),
"max_bytes": rotation.get("max_bytes", DEFAULT_MAX_BYTES),
"backup_count": rotation.get("backup_count", DEFAULT_BACKUP_COUNT),
"when": rotation.get("when", DEFAULT_WHEN),
"interval": rotation.get("interval", DEFAULT_INTERVAL),
}
def setup_logging(
logger_name: str = "douyin",
log_file: Optional[str] = None,
log_level: Optional[str] = None,
enable_console: bool = True,
enable_file: bool = True,
) -> logging.Logger:
"""
配置并返回一个增强的 logger 实例
参数:
logger_name: logger 名称
log_file: 日志文件路径(None 则从配置读取)
log_level: 日志级别(None 则从配置读取)
enable_console: 是否启用控制台输出
enable_file: 是否启用文件输出
返回:
配置好的 logger 实例
"""
logger = logging.getLogger(logger_name)
# 避免重复配置
if logger.handlers:
return logger
# 设置日志级别
if log_level is None:
log_level = get_log_level()
level = getattr(logging, log_level.upper(), logging.INFO)
logger.setLevel(level)
# 日志格式:时间戳 - [级别] - 模块名:函数名:行号 - 消息
detailed_format = (
"%(asctime)s - [%(levelname)s] - %(name)s:%(funcName)s:%(lineno)d - %(message)s"
)
simple_format = "%(asctime)s - [%(levelname)s] - %(message)s"
formatter = logging.Formatter(detailed_format, datefmt="%Y-%m-%d %H:%M:%S")
# 控制台处理器
if enable_console:
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setLevel(level)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
# 文件处理器(带轮转)
if enable_file:
if log_file is None:
log_file = get_log_file_path()
# 确保日志目录存在
log_dir = os.path.dirname(log_file)
if log_dir:
os.makedirs(log_dir, exist_ok=True)
rotation_config = get_rotation_config()
if rotation_config.get("enabled", True):
# 使用 TimedRotatingFileHandler(按时间轮转)
try:
file_handler = logging.handlers.TimedRotatingFileHandler(
log_file,
when=rotation_config.get("when", DEFAULT_WHEN),
interval=rotation_config.get("interval", DEFAULT_INTERVAL),
backupCount=rotation_config.get(
"backup_count", DEFAULT_BACKUP_COUNT
),
encoding="utf-8-sig", # 使用 BOM 避免 Windows 乱码
errors="replace",
)
except Exception:
# 如果时间轮转失败,回退到大小轮转
try:
file_handler = logging.handlers.RotatingFileHandler(
log_file,
maxBytes=rotation_config.get("max_bytes", DEFAULT_MAX_BYTES),
backupCount=rotation_config.get(
"backup_count", DEFAULT_BACKUP_COUNT
),
encoding="utf-8-sig",
errors="replace",
)
except Exception:
# 如果都失败,使用普通 FileHandler
file_handler = logging.FileHandler(
log_file,
encoding="utf-8-sig",
errors="replace",
)
else:
# 不使用轮转,直接使用 FileHandler
file_handler = logging.FileHandler(
log_file,
encoding="utf-8-sig",
errors="replace",
)
file_handler.setLevel(level)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
def get_logger(name: str = "douyin") -> logging.Logger:
"""获取或创建 logger 实例(单例模式)"""
logger = logging.getLogger(name)
if not logger.handlers:
# 如果还没有配置,进行初始化
setup_logging(name)
return logger
class LogContext:
"""日志上下文管理器,用于记录函数调用、参数、返回值、耗时等"""
def __init__(
self,
logger: logging.Logger,
func_name: str,
level: int = logging.DEBUG,
log_args: bool = True,
log_result: bool = True,
log_duration: bool = True,
):
self.logger = logger
self.func_name = func_name
self.level = level
self.log_args = log_args
self.log_result = log_result
self.log_duration = log_duration
self.start_time: Optional[float] = None
self.args: tuple = ()
self.kwargs: dict = {}
def __enter__(self):
import time
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
import time
duration = time.time() - self.start_time if self.start_time else 0
# 记录函数调用信息
if self.log_args and (self.args or self.kwargs):
args_str = ", ".join([repr(a) for a in self.args])
kwargs_str = ", ".join([f"{k}={repr(v)}" for k, v in self.kwargs.items()])
params_str = ", ".join(filter(None, [args_str, kwargs_str]))
self.logger.log(self.level, f"[函数调用] {self.func_name}({params_str})")
# 记录异常
if exc_type is not None:
self.logger.exception(
f"[函数异常] {self.func_name} 发生异常: {exc_type.__name__}",
exc_info=(exc_type, exc_val, exc_tb),
)
# 记录耗时
elif self.log_duration:
self.logger.log(
self.level, f"[函数完成] {self.func_name} 耗时: {duration:.3f}秒"
)
return False # 不抑制异常
def log_function_call(
logger: logging.Logger,
func_name: str,
args: tuple = (),
kwargs: Optional[dict] = None,
result: Any = None,
duration: Optional[float] = None,
level: int = logging.DEBUG,
):
"""记录函数调用的详细信息"""
if kwargs is None:
kwargs = {}
parts = [f"[函数调用] {func_name}"]
# 参数
if args or kwargs:
args_str = ", ".join([repr(a) for a in args])
kwargs_str = ", ".join([f"{k}={repr(v)}" for k, v in kwargs.items()])
params_str = ", ".join(filter(None, [args_str, kwargs_str]))
parts.append(f"参数: {params_str}")
# 返回值
if result is not None:
parts.append(f"返回值: {repr(result)}")
# 耗时
if duration is not None:
parts.append(f"耗时: {duration:.3f}秒")
logger.log(level, " | ".join(parts))
# 全局 logger 实例(延迟初始化)
_global_logger: Optional[logging.Logger] = None
def get_global_logger() -> logging.Logger:
"""获取全局 logger 实例"""
global _global_logger
if _global_logger is None:
_global_logger = setup_logging("douyin")
return _global_logger