feat: add log for export api
This commit is contained in:
@@ -2,11 +2,15 @@
|
||||
|
||||
import logging
|
||||
import logging.handlers
|
||||
from contextvars import ContextVar
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from app.core.config import get_settings
|
||||
|
||||
# Context variable to hold the current request_id across async boundaries
|
||||
request_id_ctx: ContextVar[str] = ContextVar("request_id", default="-")
|
||||
|
||||
|
||||
class TimedRotatingAndSizeFileHandler(logging.handlers.TimedRotatingFileHandler):
|
||||
"""File handler that rotates by both time (daily) and size (100MB)."""
|
||||
@@ -92,14 +96,13 @@ def setup_logging(log_dir: str | None = None) -> logging.Logger:
|
||||
# Remove existing handlers to avoid duplicates
|
||||
logger.handlers.clear()
|
||||
|
||||
# Create custom formatter that handles missing request_id
|
||||
# Create custom formatter that automatically injects request_id from context
|
||||
class RequestIDFormatter(logging.Formatter):
|
||||
"""Formatter that handles request_id in log records."""
|
||||
"""Formatter that injects request_id from ContextVar into log records."""
|
||||
|
||||
def format(self, record):
|
||||
# Add request_id if not present
|
||||
if not hasattr(record, "request_id"):
|
||||
record.request_id = getattr(record, "request_id", "unknown")
|
||||
record.request_id = request_id_ctx.get()
|
||||
return super().format(record)
|
||||
|
||||
formatter = RequestIDFormatter(
|
||||
|
||||
Reference in New Issue
Block a user