Some checks are pending
Build and Push Docker Images / changes (push) Waiting to run
Build and Push Docker Images / build-backend (push) Blocked by required conditions
Build and Push Docker Images / build-frontend (push) Blocked by required conditions
Build and Push Docker Images / build-admin-frontend (push) Blocked by required conditions
49 lines
1.3 KiB
Python
49 lines
1.3 KiB
Python
"""结构化日志配置。"""
|
|
|
|
import logging
|
|
import sys
|
|
|
|
import structlog
|
|
|
|
from app.core.config import settings
|
|
|
|
|
|
def setup_logging():
|
|
"""配置 structlog 结构化日志。"""
|
|
shared_processors = [
|
|
structlog.contextvars.merge_contextvars,
|
|
structlog.stdlib.add_log_level,
|
|
structlog.stdlib.add_logger_name,
|
|
structlog.processors.TimeStamper(fmt="iso"),
|
|
structlog.processors.StackInfoRenderer(),
|
|
]
|
|
|
|
if settings.debug:
|
|
processors = shared_processors + [
|
|
structlog.dev.ConsoleRenderer(colors=True),
|
|
]
|
|
else:
|
|
processors = shared_processors + [
|
|
structlog.processors.format_exc_info,
|
|
structlog.processors.JSONRenderer(),
|
|
]
|
|
|
|
structlog.configure(
|
|
processors=processors,
|
|
wrapper_class=structlog.stdlib.BoundLogger,
|
|
context_class=dict,
|
|
logger_factory=structlog.stdlib.LoggerFactory(),
|
|
cache_logger_on_first_use=True,
|
|
)
|
|
|
|
logging.basicConfig(
|
|
format="%(message)s",
|
|
stream=sys.stdout,
|
|
level=logging.DEBUG if settings.debug else logging.INFO,
|
|
)
|
|
|
|
|
|
def get_logger(name: str) -> structlog.stdlib.BoundLogger:
|
|
"""获取结构化日志器。"""
|
|
return structlog.get_logger(name)
|