wechat_msg_clicker/wechat_clicker/logger_setup.py
2026-04-22 19:28:54 +08:00

41 lines
1.1 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.

"""日志配置"""
import logging
from logging.handlers import RotatingFileHandler
def setup_logging(config) -> logging.Logger:
"""配置日志系统,返回 logger 实例。"""
logger = logging.getLogger("wechat_clicker")
logger.setLevel(getattr(logging, config.log_level, logging.INFO))
# 避免重复添加 handler
if logger.handlers:
return logger
formatter = logging.Formatter(
"%(asctime)s [%(levelname)s] %(name)s: %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
# 文件 handler自动轮转
file_handler = RotatingFileHandler(
config.log_file,
maxBytes=config.log_max_bytes,
backupCount=config.log_backup_count,
encoding="utf-8",
)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
# 控制台 handler
if config.log_console:
console_handler = logging.StreamHandler()
console_handler.setFormatter(logging.Formatter(
"%(asctime)s [%(levelname)s] %(message)s",
datefmt="%H:%M:%S",
))
logger.addHandler(console_handler)
return logger