调整 日志输出格式

This commit is contained in:
2026-04-29 16:28:23 +08:00
parent 8e4c2eef98
commit 02b3470075
8 changed files with 303 additions and 142 deletions
+52 -27
View File
@@ -3,10 +3,14 @@ from __future__ import annotations
import argparse
import json
import logging
import re
import os
import time
from typing import Any
from inventory_analytics.logging_utils import exception_fields
from inventory_analytics.logging_utils import log_event as emit_log_event
from inventory_analytics.logging_utils import sanitize_value
from .feishu import FeishuClient
from .postgres import query_postgres
from .service import run_feishu_summary, run_sync_brand
@@ -14,33 +18,24 @@ from .settings import build_app_config
logger = logging.getLogger("inventory_sync.cli")
SENSITIVE_PATTERN = re.compile(r"(?i)(password|passwd|secret|token|authorization)\s*[:=]\s*([^\s,;]+)")
LOG_FIELD_LABELS = {
"action": "动作",
"elapsed_ms": "耗时毫秒",
"env_path": "环境文件",
"error": "错误",
"error_type": "异常类型",
"fallback": "兜底方式",
"loaded": "已加载",
"row_count": "行数",
"source_row_count": "源数据行数",
"stage": "阶段",
"table_count": "表数量",
"target_count": "目标数量",
}
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 "")
emit_log_event(logger, LOG_FIELD_LABELS, level, event, **fields)
def parse_args() -> argparse.Namespace:
@@ -54,60 +49,90 @@ def parse_args() -> argparse.Namespace:
return parser.parse_args()
def safe_result_summary(result: dict[str, object]) -> dict[str, object]:
summary: dict[str, object] = {}
feishu_result = result.get("feishu")
if isinstance(feishu_result, dict):
summary["feishu"] = {"table_count": feishu_result.get("table_count")}
postgres_result = result.get("postgres")
if isinstance(postgres_result, dict):
summary["postgres"] = {"row_count": postgres_result.get("row_count")}
sync_result = result.get("sync_brand")
if isinstance(sync_result, dict):
summary["sync_brand"] = {
"target_count": sync_result.get("target_count"),
"source_row_count": sync_result.get("source_row_count"),
}
return summary
def public_result(result: dict[str, object]) -> dict[str, object]:
return sanitize_value(result)
def main() -> int:
started_at = time.monotonic()
try:
args = parse_args()
log_event(logging.INFO, "飞书同步动作开始", action=args.action)
safe_logs = os.getenv("INVENTORY_SAFE_LOGS") == "1"
log_event(logging.INFO, "渠道库存同步任务启动", action=args.action, stage="start")
config = build_app_config()
if config.env_path:
log_event(logging.INFO, "已加载环境变量文件", env_path=config.env_path)
log_event(logging.INFO, "配置加载完成", loaded=True, stage="config")
else:
log_event(logging.WARNING, "未找到环境变量文件", fallback="当前Shell环境变量")
log_event(logging.WARNING, "未找到环境变量文件,使用当前Shell环境变量", fallback="shell", stage="config")
client = FeishuClient(config)
result: dict[str, object] = {}
if args.action in {"feishu", "all"}:
log_event(logging.INFO, "开始读取飞书Base信息")
log_event(logging.INFO, "开始读取飞书表信息", stage="feishu")
result["feishu"] = run_feishu_summary(client)
log_event(
logging.INFO,
"飞书Base信息读取完成",
"飞书表信息读取完成",
stage="feishu",
table_count=result["feishu"].get("table_count") if isinstance(result["feishu"], dict) else None,
)
if args.action in {"postgres", "all"}:
log_event(logging.INFO, "开始查询PostgreSQL")
log_event(logging.INFO, "开始查询渠道库存数据", stage="database")
result["postgres"] = query_postgres(config)
log_event(
logging.INFO,
"PostgreSQL查询完成",
"渠道库存数据查询完成",
stage="database",
row_count=result["postgres"].get("row_count") if isinstance(result["postgres"], dict) else None,
)
if args.action in {"sync-brand", "all"}:
log_event(logging.INFO, "开始同步品牌数据")
log_event(logging.INFO, "开始同步渠道库存到飞书", stage="sync")
postgres_result = result.get("postgres")
result["sync_brand"] = run_sync_brand(config, client, postgres_result if isinstance(postgres_result, dict) else None)
log_event(
logging.INFO,
"品牌数据同步完成",
"渠道库存同步到飞书完成",
stage="sync",
target_count=result["sync_brand"].get("target_count") if isinstance(result["sync_brand"], dict) else None,
source_row_count=result["sync_brand"].get("source_row_count") if isinstance(result["sync_brand"], dict) else None,
)
print(json.dumps(result, ensure_ascii=False, indent=2))
if safe_logs:
print(json.dumps(safe_result_summary(result), ensure_ascii=False, indent=2))
else:
print(json.dumps(public_result(result), ensure_ascii=False, indent=2))
log_event(
logging.INFO,
"飞书同步动作完成",
"渠道库存同步任务完成",
action=args.action,
stage="finish",
elapsed_ms=int((time.monotonic() - started_at) * 1000),
)
return 0
except Exception as exc: # noqa: BLE001
log_event(
logging.ERROR,
"飞书同步动作失败",
error=exc,
"渠道库存同步任务失败",
stage="failed",
**exception_fields(exc),
elapsed_ms=int((time.monotonic() - started_at) * 1000),
)
return 1
+15 -31
View File
@@ -1,9 +1,10 @@
from __future__ import annotations
import logging
import re
from typing import Any
from inventory_analytics.logging_utils import log_event as emit_log_event
from .feishu import FeishuApiError, FeishuClient
from .postgres import query_postgres
from .settings import AppConfig
@@ -11,7 +12,6 @@ from .specs import BRAND_SYNC_TARGETS, FIELD_SPECS, PRIMARY_FIELD_NAME, PRIMARY_
logger = logging.getLogger("inventory_sync.service")
SENSITIVE_PATTERN = re.compile(r"(?i)(password|passwd|secret|token|authorization)\s*[:=]\s*([^\s,;]+)")
LOG_FIELD_LABELS = {
"advanced_permission_enabled": "高级权限已开启",
@@ -20,26 +20,16 @@ LOG_FIELD_LABELS = {
"deleted_records": "删除记录数",
"existing_records": "已有记录数",
"rows": "行数",
"selected_table_id": "选中表ID",
"source_rows": "源数据行数",
"stage": "阶段",
"table_count": "表数量",
"table_name": "表名",
"target_count": "目标数量",
}
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 "")
emit_log_event(logger, LOG_FIELD_LABELS, level, event, **fields)
def build_feishu_record(row: dict[str, Any]) -> dict[str, Any]:
@@ -64,34 +54,24 @@ def build_feishu_record(row: dict[str, Any]) -> dict[str, Any]:
def run_feishu_summary(client: FeishuClient) -> dict[str, Any]:
log_event(logging.INFO, "开始获取飞书租户访问令牌")
log_event(logging.INFO, "开始获取飞书访问令牌", stage="feishu_auth")
access_token = client.get_tenant_access_token()
log_event(logging.INFO, "飞书租户访问令牌获取完成")
log_event(logging.INFO, "飞书访问令牌获取完成", stage="feishu_auth")
base_info = client.parse_base_info(client.build_base_url(), access_token)
log_event(logging.INFO, "飞书Base信息解析完成", selected_table_id=base_info["selected_table_id"])
log_event(logging.INFO, "飞书Base信息解析完成", stage="feishu_base")
advanced_permission_enabled = client.enable_advanced_permission(access_token, base_info["app_token"] or "")
tables = client.list_tables(access_token, base_info["app_token"] or "")
log_event(
logging.INFO,
"飞书Base表列表读取完成",
stage="feishu_tables",
table_count=len(tables),
advanced_permission_enabled=advanced_permission_enabled,
)
return {
"base_url": base_info["base_url"],
"app_token": base_info["app_token"],
"auth_mode": "tenant_access_token",
"selected_table_id": base_info["selected_table_id"],
"selected_view_id": base_info["selected_view_id"],
"advanced_permission_enabled": advanced_permission_enabled,
"folder": {
"name": base_info["app_token"],
"type": "base",
"tables": [
{"table_id": table.get("table_id"), "name": table.get("name"), "revision": table.get("revision")}
for table in tables
],
},
"tables": [{"name": table.get("name"), "revision": table.get("revision")} for table in tables],
"table_count": len(tables),
}
@@ -107,6 +87,7 @@ def sync_brand_target(
log_event(
logging.INFO,
"开始同步目标飞书表",
stage="sync_target",
brand_name=target.brand_name,
table_name=target.table_name,
source_rows=len(postgres_rows or []),
@@ -128,6 +109,7 @@ def sync_brand_target(
log_event(
logging.INFO,
"目标飞书表已有记录读取完成",
stage="sync_target",
brand_name=target.brand_name,
table_name=target.table_name,
existing_records=len(existing_record_ids),
@@ -142,6 +124,7 @@ def sync_brand_target(
log_event(
logging.INFO,
"目标飞书表同步完成",
stage="sync_target",
brand_name=target.brand_name,
table_name=target.table_name,
deleted_records=deleted_count,
@@ -162,13 +145,13 @@ def sync_brand_target(
def run_sync_brand(config: AppConfig, client: FeishuClient, postgres_result: dict[str, Any] | None = None) -> dict[str, Any]:
log_event(logging.INFO, "开始准备品牌同步", target_count=len(BRAND_SYNC_TARGETS))
log_event(logging.INFO, "开始准备品牌同步", stage="sync_prepare", target_count=len(BRAND_SYNC_TARGETS))
access_token = client.get_tenant_access_token()
base_info = client.parse_base_info(client.build_base_url(), access_token)
app_token = base_info["app_token"] or ""
client.enable_advanced_permission(access_token, app_token)
tables = client.list_tables(access_token, app_token)
log_event(logging.INFO, "品牌同步准备完成", table_count=len(tables))
log_event(logging.INFO, "品牌同步准备完成", stage="sync_prepare", table_count=len(tables))
results = []
source_row_count = 0
@@ -181,6 +164,7 @@ def run_sync_brand(config: AppConfig, client: FeishuClient, postgres_result: dic
log_event(
logging.INFO,
"目标品牌PostgreSQL数据已准备",
stage="sync_prepare",
brand_name=target.brand_name,
table_name=target.table_name,
rows=len(postgres_rows or []),