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
This commit is contained in:
@@ -5,12 +5,13 @@ by keeping the cache and its helpers in a dedicated module.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
|
||||
import structlog
|
||||
|
||||
from app.database.database import AsyncSessionLocal
|
||||
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
logger = structlog.get_logger(__name__)
|
||||
|
||||
# ---- Defaults per section ------------------------------------------------
|
||||
|
||||
@@ -116,5 +117,5 @@ async def load_button_styles_cache() -> dict[str, dict]:
|
||||
logger.exception('Failed to load button styles from DB, using defaults')
|
||||
|
||||
_cached_styles = merged
|
||||
logger.info('Button styles cache loaded: %s', list(merged.keys()))
|
||||
logger.info('Button styles cache loaded', list=list(merged.keys()))
|
||||
return merged
|
||||
|
||||
Reference in New Issue
Block a user