Files
leaudit-platform-backend/fastapi_common/fastapi_common_logger/__init__.py
T
wren 535d97a70c chore: initial commit — leaudit-platform project skeleton
17-table PostgreSQL schema with full Chinese column comments,
FastAPI project structure (admin/common/modules),
DSL rule files, and schema migration scripts.
2026-04-27 16:48:22 +08:00

44 lines
1.1 KiB
Python

"""日志模块 —— 通道由调用栈自动推断。"""
from __future__ import annotations
import logging
import sys
def _infer_channel() -> str:
"""根据调用栈自动推断日志通道。"""
frame = sys._getframe(2)
filename = frame.f_code.co_filename
if "/controllers/" in filename:
return "CONTROLLER"
if "/services/" in filename:
return "SERVICE"
if "/models/" in filename:
return "MODEL"
if "/leaudit_bridge/" in filename:
return "BRIDGE"
if "/handler/" in filename:
return "HANDLER"
if "/tasks/" in filename:
return "TASK"
if "/middleware/" in filename:
return "MIDDLEWARE"
if "postgrest" in filename:
return "POSTGREST"
if "uvicorn" in filename:
return "UVICORN"
return "APP"
class _ChannelLogger:
"""代理 logger,自动推断通道。"""
def __getattr__(self, name: str):
channel = _infer_channel()
logger = logging.getLogger(channel)
return getattr(logger, name)
logger: object = _ChannelLogger() # type: ignore[assignment]