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:
Fringg
2026-02-16 09:18:12 +03:00
parent 871ceb866c
commit 1f0fef114b
294 changed files with 7144 additions and 6713 deletions
+4 -10
View File
@@ -1,6 +1,5 @@
import html
import io
import logging
import math
import time
from collections.abc import Iterable
@@ -693,17 +692,12 @@ async def apply_preset(
await bot_configuration_service.set_value(db, setting_key, value)
applied.append(setting_key)
except ReadOnlySettingError:
logging.getLogger(__name__).info(
'Пропускаем настройку %s из пресета %s: только для чтения',
setting_key,
preset_key,
logger.info(
'Пропускаем настройку из пресета : только для чтения', setting_key=setting_key, preset_key=preset_key
)
except Exception as error:
logging.getLogger(__name__).warning(
'Не удалось применить пресет %s для %s: %s',
preset_key,
setting_key,
error,
logger.warning(
'Не удалось применить пресет для', preset_key=preset_key, setting_key=setting_key, error=error
)
await db.commit()