重构
This commit is contained in:
@@ -1,104 +1,7 @@
|
||||
#!/usr/bin/env python3
|
||||
"""CLI entrypoint for inventory sync."""
|
||||
"""Compatibility entrypoint for channel inventory sync and Feishu table inspection."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from pathlib import Path
|
||||
import re
|
||||
import sys
|
||||
import time
|
||||
from typing import Any
|
||||
|
||||
from inventory_sync.cli import main as inventory_sync_main
|
||||
|
||||
|
||||
LOGGER_NAME = "inventory_sync_entrypoint"
|
||||
logger = logging.getLogger(LOGGER_NAME)
|
||||
PROJECT_ROOT = Path(__file__).resolve().parent
|
||||
LOG_DIR = PROJECT_ROOT / "logs"
|
||||
LOG_FILE = LOG_DIR / "list_feishu_base_tables.log"
|
||||
SENSITIVE_PATTERN = re.compile(r"(?i)(password|passwd|secret|token|authorization)\s*[:=]\s*([^\s,;]+)")
|
||||
|
||||
LOG_FIELD_LABELS = {
|
||||
"argv": "参数",
|
||||
"cwd": "工作目录",
|
||||
"elapsed_ms": "耗时毫秒",
|
||||
"error": "错误",
|
||||
"exit_code": "退出码",
|
||||
"log_file": "日志文件",
|
||||
}
|
||||
|
||||
|
||||
def configure_logging() -> None:
|
||||
LOG_DIR.mkdir(parents=True, exist_ok=True)
|
||||
formatter = logging.Formatter(
|
||||
fmt="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
datefmt="%Y-%m-%d %H:%M:%S",
|
||||
)
|
||||
stream_handler = logging.StreamHandler(sys.stderr)
|
||||
stream_handler.setFormatter(formatter)
|
||||
file_handler = RotatingFileHandler(LOG_FILE, maxBytes=5 * 1024 * 1024, backupCount=5, encoding="utf-8")
|
||||
file_handler.setFormatter(formatter)
|
||||
root_logger = logging.getLogger()
|
||||
root_logger.handlers.clear()
|
||||
root_logger.setLevel(logging.INFO)
|
||||
root_logger.addHandler(stream_handler)
|
||||
root_logger.addHandler(file_handler)
|
||||
|
||||
|
||||
def sanitize_value(value: Any) -> Any:
|
||||
if isinstance(value, str):
|
||||
return SENSITIVE_PATTERN.sub(r"\1=***", value)
|
||||
return value
|
||||
|
||||
|
||||
def log_event(level: int, event: str, **fields: Any) -> None:
|
||||
safe_event = sanitize_value(event)
|
||||
details = " ".join(
|
||||
f"{LOG_FIELD_LABELS.get(key, key)}={sanitize_value(value)!r}" for key, value in fields.items()
|
||||
)
|
||||
logger.log(level, "事件=%s%s", safe_event, f" {details}" if details else "")
|
||||
|
||||
|
||||
def main() -> int:
|
||||
configure_logging()
|
||||
started_at = time.monotonic()
|
||||
log_event(
|
||||
logging.INFO,
|
||||
"飞书同步入口启动",
|
||||
argv=sys.argv[1:],
|
||||
cwd=Path.cwd(),
|
||||
log_file=LOG_FILE,
|
||||
)
|
||||
try:
|
||||
exit_code = inventory_sync_main()
|
||||
except SystemExit as exc:
|
||||
exit_code = int(exc.code or 0) if isinstance(exc.code, int) else 1
|
||||
log_event(
|
||||
logging.INFO if exit_code == 0 else logging.ERROR,
|
||||
"飞书同步入口主动退出",
|
||||
exit_code=exit_code,
|
||||
elapsed_ms=int((time.monotonic() - started_at) * 1000),
|
||||
)
|
||||
return exit_code
|
||||
except Exception as exc:
|
||||
logger.exception(
|
||||
"事件=%s 错误=%r 耗时毫秒=%r",
|
||||
"飞书同步入口异常",
|
||||
sanitize_value(str(exc)),
|
||||
int((time.monotonic() - started_at) * 1000),
|
||||
)
|
||||
return 1
|
||||
|
||||
log_event(
|
||||
logging.INFO if exit_code == 0 else logging.ERROR,
|
||||
"飞书同步入口完成",
|
||||
exit_code=exit_code,
|
||||
elapsed_ms=int((time.monotonic() - started_at) * 1000),
|
||||
)
|
||||
return exit_code
|
||||
from inventory_analytics.channel_inventory_sync.entrypoint import main
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user