feat(onboarding): 新增重置新手引导端点 /api/v1/user/onboarding/reset (#114)
Reviewed-on: #114
This commit was merged in pull request #114.
This commit is contained in:
+57
-8
@@ -2,9 +2,15 @@
|
||||
|
||||
业务代码用 `logger = logging.getLogger("shagua.xxx")` 即可, 本模块在 main.py 启动时调一次。
|
||||
|
||||
- 控制台(stdout): 人类可读文本, 给 systemd / 本地看。
|
||||
- 文件 `logs/app-server.log`: 单行 JSON, 供阿里云 SLS/Logtail 采集(JSON 模式零正则);
|
||||
异常栈作为字段内嵌不换行 → 每条日志一行。
|
||||
- 控制台(stdout): 人类可读文本, 给 systemd / 本地看; 有 trace 时行尾附 `trace=xxx`。
|
||||
- 文件 `logs/app-server.log`: 单行 JSON, 供**阿里云 SLS / Logtail** 采集(JSON 模式零正则);
|
||||
异常栈内嵌为字段不换行 → 每条日志一行。
|
||||
- **结构化字段(SLS 可直接查/聚合)**:
|
||||
- `trace_id`: 请求级贯穿——在入口 `trace_id_ctx.set(...)` 后, 本请求内**每一行日志**(含
|
||||
run_in_threadpool 里的 harvest, contextvars 自动拷进线程)都自动带上, 无需手写。
|
||||
SLS 里 `trace_id: "xxx"` 一查即得整条比价链路, 按 time 升序即请求顺序。
|
||||
- 任意 `logger.info(msg, extra={"phase": ..., "step": ..., "command": ...})` 的 extra
|
||||
键都会平铺进 JSON 顶层 → SLS 可按 phase/step/command/cost_ms 等过滤聚合。
|
||||
- 环境变量:
|
||||
- LOG_JSON_CONSOLE=1 控制台也输出 JSON
|
||||
- LOG_DIR / LOG_FILE 改落盘路径(默认 logs/app-server.log)
|
||||
@@ -17,13 +23,36 @@ import json
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from contextvars import ContextVar
|
||||
from datetime import datetime
|
||||
from logging.handlers import RotatingFileHandler
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
# 请求级 trace_id:入口(如 compare.py 透传壳)set 之后, 本请求上下文(含 run_in_threadpool
|
||||
# 拷贝出去的线程)内所有日志自动带上。默认空串 = 非请求上下文(启动/后台 worker)。
|
||||
trace_id_ctx: ContextVar[str] = ContextVar("trace_id", default="")
|
||||
|
||||
|
||||
# 标准 LogRecord 属性 + 格式化期附加项:凡不在此集合的 record 属性都视为业务 extra, 平铺进 JSON。
|
||||
_RESERVED = set(
|
||||
logging.LogRecord("", 0, "", 0, "", (), None).__dict__
|
||||
) | {"message", "asctime", "trace_id", "taskName"}
|
||||
|
||||
|
||||
class _ContextFilter(logging.Filter):
|
||||
"""把 trace_id_ctx 注入每条 record(供两个 formatter 取用)。挂在 handler 上,
|
||||
命中每条(含 propagate 上来的)记录, 在 format 之前置好 record.trace_id。"""
|
||||
|
||||
def filter(self, record: logging.LogRecord) -> bool:
|
||||
if not hasattr(record, "trace_id"):
|
||||
record.trace_id = trace_id_ctx.get()
|
||||
return True
|
||||
|
||||
|
||||
class JsonFormatter(logging.Formatter):
|
||||
"""把 LogRecord 序列化成单行 JSON(SLS/Logtail 友好)。异常栈内嵌为字段, 整条仍是一行。"""
|
||||
"""LogRecord → 单行 JSON(SLS/Logtail 友好)。trace_id 提到顶层、extra 键平铺, 异常栈内嵌。"""
|
||||
|
||||
def __init__(self, service: str = "app-server"):
|
||||
super().__init__()
|
||||
self.service = service
|
||||
@@ -35,10 +64,17 @@ class JsonFormatter(logging.Formatter):
|
||||
"level": record.levelname,
|
||||
"service": self.service,
|
||||
"logger": record.name,
|
||||
"func": record.funcName,
|
||||
"line": record.lineno,
|
||||
"message": record.getMessage(),
|
||||
}
|
||||
tid = getattr(record, "trace_id", "") or trace_id_ctx.get()
|
||||
if tid:
|
||||
data["trace_id"] = tid
|
||||
# 业务 extra 字段(phase / step / endpoint / command / cost_ms / status ...)平铺进顶层
|
||||
for k, v in record.__dict__.items():
|
||||
if k not in _RESERVED and not k.startswith("_"):
|
||||
data[k] = v
|
||||
data["func"] = record.funcName
|
||||
data["line"] = record.lineno
|
||||
data["message"] = record.getMessage()
|
||||
if record.exc_info:
|
||||
data["exception"] = self.formatException(record.exc_info)
|
||||
if record.stack_info:
|
||||
@@ -46,6 +82,15 @@ class JsonFormatter(logging.Formatter):
|
||||
return json.dumps(data, ensure_ascii=False, default=str)
|
||||
|
||||
|
||||
class TextFormatter(logging.Formatter):
|
||||
"""控制台文本:标准行 + 有 trace_id 时行尾附 `trace=xxx`(无 trace 的启动/后台日志不加噪)。"""
|
||||
|
||||
def format(self, record: logging.LogRecord) -> str:
|
||||
base = super().format(record)
|
||||
tid = getattr(record, "trace_id", "") or trace_id_ctx.get()
|
||||
return f"{base} trace={tid}" if tid else base
|
||||
|
||||
|
||||
_CONFIGURED = False
|
||||
|
||||
|
||||
@@ -63,14 +108,17 @@ def setup_logging(debug: bool = False) -> None:
|
||||
for h in list(root.handlers):
|
||||
root.removeHandler(h)
|
||||
|
||||
ctx_filter = _ContextFilter()
|
||||
|
||||
# 控制台: 默认文本(systemd/本地看); LOG_JSON_CONSOLE=1 时输出 JSON
|
||||
console = logging.StreamHandler(sys.stdout)
|
||||
if os.getenv("LOG_JSON_CONSOLE") == "1":
|
||||
console.setFormatter(JsonFormatter(service))
|
||||
else:
|
||||
console.setFormatter(
|
||||
logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
|
||||
TextFormatter("%(asctime)s %(levelname)s %(name)s: %(message)s")
|
||||
)
|
||||
console.addFilter(ctx_filter)
|
||||
root.addHandler(console)
|
||||
|
||||
# 文件: 单行 JSON, 供 Logtail 采集(自动轮转, 单文件 10MB, 保留 5 个)
|
||||
@@ -82,6 +130,7 @@ def setup_logging(debug: bool = False) -> None:
|
||||
log_file, maxBytes=10 * 1024 * 1024, backupCount=5, encoding="utf-8",
|
||||
)
|
||||
file_handler.setFormatter(JsonFormatter(service))
|
||||
file_handler.addFilter(ctx_filter)
|
||||
root.addHandler(file_handler)
|
||||
|
||||
# 第三方库降噪
|
||||
|
||||
Reference in New Issue
Block a user