v1进行时:UpsertUserAll回归基础包体(附加数据一律走extra_data)、解锁位串默认0、适配2026-10服务器维护(token一次性、上传冷却50s、买票冷却40s写入config)、新增落盘与日志开关
This commit is contained in:
1 parent
1d365cc948
commit
ed7d463519
12 files changed
+552
-271
No files matched your search
+43
-7
@@ -38,6 +38,15 @@ LogSetup.py —— 统一日志模块(INFO / DEBUG 双模式)
|
||||
|
||||
每行都带 [YYYY-MM-DD HH:MM:SS] 时间戳。
|
||||
|
||||
【要不要生成日志文件】
|
||||
|
||||
优先级: setup_logging(enabled=...) 显式传参 > config.py 的 LOG_ENABLED > 开
|
||||
|
||||
LOG_ENABLED = True # 照常写两个日志文件
|
||||
LOG_ENABLED = False # 一个字节都不落盘(控制台输出照旧)
|
||||
|
||||
单次运行想临时关掉:setup_logging("名字", enabled=False)
|
||||
|
||||
【主脚本怎么用】
|
||||
|
||||
from api import setup_logging, teardown_logging, ParseQRCode, UserLogin
|
||||
@@ -101,6 +110,8 @@ _session_started = False
|
||||
_session_script_name = ""
|
||||
_current_level = LEVEL_INFO
|
||||
_value_max = 0
|
||||
# 是否写日志文件(False = 一个字节都不落盘,控制台照旧)
|
||||
_enabled = True
|
||||
|
||||
_date_log_path: Optional[str] = None
|
||||
_latest_log_path: Optional[str] = None
|
||||
@@ -145,6 +156,15 @@ def _config_value_max() -> int:
|
||||
return 0
|
||||
|
||||
|
||||
def _config_enabled() -> bool:
|
||||
"""从 config.py 读 LOG_ENABLED;读不到就当 True(默认照常记日志)。"""
|
||||
try:
|
||||
import config # type: ignore
|
||||
return bool(getattr(config, "LOG_ENABLED", True))
|
||||
except Exception:
|
||||
return True
|
||||
|
||||
|
||||
def get_level() -> str:
|
||||
"""返回当前日志模式('info' / 'debug')。"""
|
||||
return _current_level
|
||||
@@ -155,6 +175,11 @@ def is_debug() -> bool:
|
||||
return _LEVEL_RANK.get(_current_level, 20) <= _LEVEL_RANK[LEVEL_DEBUG]
|
||||
|
||||
|
||||
def is_log_enabled() -> bool:
|
||||
"""当前是否写日志文件(config.LOG_ENABLED / setup_logging(enabled=...))。"""
|
||||
return bool(_enabled)
|
||||
|
||||
|
||||
def set_level(level: Any) -> str:
|
||||
"""运行时切换模式(一般不用,改 config.py 即可)。返回切换后的模式。"""
|
||||
global _current_level
|
||||
@@ -340,40 +365,49 @@ def get_called_apis() -> list:
|
||||
# 会话:初始化 / 收尾
|
||||
# =============================================================================
|
||||
|
||||
def setup_logging(script_name: str, level: Any = None) -> str:
|
||||
def setup_logging(script_name: str, level: Any = None,
|
||||
enabled: Optional[bool] = None) -> str:
|
||||
"""
|
||||
初始化日志会话(主脚本开头调用一次)。
|
||||
|
||||
做三件事:
|
||||
1. 确定日志模式(level 显式传参 > config.LOG_LEVEL > INFO)
|
||||
2. 打开两个日志文件:日期日志(追加)+ latest.log(覆盖)
|
||||
(enabled=False 或 config.LOG_ENABLED=False 时一个都不开)
|
||||
3. 打印会话横幅
|
||||
|
||||
参数:
|
||||
script_name: 脚本名,显示在横幅上
|
||||
level: 日志模式;None(默认)= 读 config.py 的 LOG_LEVEL
|
||||
enabled: 是否写日志文件;None(默认)= 读 config.py 的 LOG_ENABLED
|
||||
False = 不创建、不写任何日志文件(控制台输出照旧)
|
||||
|
||||
返回:
|
||||
实际生效的模式('info' / 'debug')
|
||||
"""
|
||||
global _session_started, _session_script_name, _value_max
|
||||
global _session_started, _session_script_name, _value_max, _enabled
|
||||
global _date_log_path, _latest_log_path, _date_log_file, _latest_log_file
|
||||
|
||||
set_level(level if level is not None else _config_level())
|
||||
_value_max = _config_value_max()
|
||||
_enabled = _config_enabled() if enabled is None else bool(enabled)
|
||||
|
||||
if _session_started:
|
||||
return _current_level
|
||||
|
||||
_ensure_logs_dir()
|
||||
_session_script_name = script_name
|
||||
_date_log_path = _get_date_log_path()
|
||||
_latest_log_path = _get_latest_log_path()
|
||||
|
||||
_called_apis.clear()
|
||||
|
||||
_date_log_file = open(_date_log_path, "a", encoding="utf-8") # 追加:当日全部
|
||||
_latest_log_file = open(_latest_log_path, "w", encoding="utf-8") # 覆盖:仅当次
|
||||
if _enabled:
|
||||
_ensure_logs_dir()
|
||||
_date_log_file = open(_date_log_path, "a", encoding="utf-8") # 追加:当日全部
|
||||
_latest_log_file = open(_latest_log_path, "w", encoding="utf-8") # 覆盖:仅当次
|
||||
else:
|
||||
_date_log_file = None
|
||||
_latest_log_file = None
|
||||
|
||||
_print_banner(script_name)
|
||||
_session_started = True
|
||||
@@ -394,8 +428,10 @@ def _print_banner(script_name: str) -> None:
|
||||
_INDENT * 2 + "工作目录 : " + os.getcwd(),
|
||||
_INDENT * 2 + "已挂载 api : " + str(len(_tracked_api_names))
|
||||
+ " 个(自动跟踪,主脚本无需写日志代码)",
|
||||
_INDENT * 2 + "当次日志 : " + str(_latest_log_path),
|
||||
_INDENT * 2 + "日期日志 : " + str(_date_log_path),
|
||||
_INDENT * 2 + "当次日志 : "
|
||||
+ (str(_latest_log_path) if _enabled else "已关闭(不写日志文件)"),
|
||||
_INDENT * 2 + "日期日志 : "
|
||||
+ (str(_date_log_path) if _enabled else "已关闭(config.LOG_ENABLED=False)"),
|
||||
_SEPARATOR,
|
||||
]
|
||||
_emit("\n".join(lines))
|
||||
|
||||
Reference in new issue
Block a user