Files
remnawave-bedolaga-telegram…/app/webapi/middleware.py
Fringg 1f0fef114b refactor: complete structlog migration with contextvars, kwargs, and logging hardening
- Add ContextVarsMiddleware for automatic user_id/chat_id/username binding
  via structlog contextvars (aiogram) and http_method/http_path (FastAPI)
- Use bound_contextvars() context manager instead of clear_contextvars()
  to safely restore previous state instead of wiping all context
- Register ContextVarsMiddleware as outermost middleware (before GlobalError)
  so all error logs include user context
- Replace structlog.get_logger() with structlog.get_logger(__name__) across
  270 calls in 265 files for meaningful logger names
- Switch wrapper_class from BoundLogger to make_filtering_bound_logger()
  for pre-processor level filtering (performance optimization)
- Migrate 1411 %-style positional arg logger calls to structlog kwargs
  style across 161 files via AST script
- Migrate log_rotation_service.py from stdlib logging to structlog
- Add payment module prefixes to TelegramNotifierProcessor.IGNORED_LOGGER_PREFIXES
  and ExcludePaymentFilter.PAYMENT_MODULES to prevent payment data leaking
  to Telegram notifications and general log files
- Fix LoggingMiddleware: add from_user null-safety for channel posts,
  switch time.time() to time.monotonic() for duration measurement
- Remove duplicate logger assignments in purchase.py, config.py,
  inline.py, and admin/payments.py
2026-02-16 09:18:12 +03:00

41 lines
1.7 KiB
Python

from __future__ import annotations
from time import monotonic
import structlog
from sqlalchemy.exc import InterfaceError, OperationalError
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from structlog.contextvars import bound_contextvars
logger = structlog.get_logger('web_api')
class RequestLoggingMiddleware(BaseHTTPMiddleware):
"""Логирование входящих запросов в административный API."""
async def dispatch(self, request: Request, call_next: RequestResponseEndpoint) -> Response:
async with bound_contextvars(http_method=request.method, http_path=request.url.path):
start = monotonic()
response: Response | None = None
try:
response = await call_next(request)
return response
except (TimeoutError, ConnectionRefusedError, OSError, OperationalError, InterfaceError) as e:
logger.error(
'Database connection error on', method=request.method, path=request.url.path, e=str(e)[:200]
)
response = JSONResponse(
status_code=503,
content={'detail': 'Service temporarily unavailable. Please try again later.'},
)
return response
finally:
duration_ms = (monotonic() - start) * 1000
status = response.status_code if response else 'error'
logger.debug(
'-> (ms)', method=request.method, path=request.url.path, status=status, duration_ms=duration_ms
)