mirror of
https://github.com/BEDOLAGA-DEV/remnawave-bedolaga-telegram-bot.git
synced 2026-02-28 07:11:37 +00:00
- 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
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
"""Specialized logger for payment operations.
|
|
|
|
Provides a dedicated structlog logger for all payment-related events.
|
|
The underlying stdlib logger backend writes to a separate ``payments.log``
|
|
file with ``propagate=False`` to avoid duplicating payment entries in the
|
|
general log.
|
|
|
|
Usage::
|
|
|
|
from app.utils.payment_logger import payment_logger
|
|
|
|
payment_logger.info('Created YooKassa payment', payment_id=payment_id, amount=amount)
|
|
payment_logger.error('Webhook processing error', error=error)
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
import structlog
|
|
|
|
|
|
# The stdlib backend logger — keeps propagate=False so payment events
|
|
# only go to the dedicated file handler, not the root logger.
|
|
_stdlib_payment_logger = logging.getLogger('app.payments')
|
|
|
|
# Structlog frontend wrapping the stdlib backend.
|
|
# All structlog processors (timestamper, notifier, etc.) apply normally.
|
|
payment_logger: structlog.stdlib.BoundLogger = structlog.wrap_logger(
|
|
_stdlib_payment_logger,
|
|
wrapper_class=structlog.stdlib.BoundLogger,
|
|
)
|
|
|
|
|
|
def configure_payment_logger(
|
|
handler: logging.Handler,
|
|
formatter: logging.Formatter | None = None,
|
|
level: int = logging.INFO,
|
|
) -> None:
|
|
"""Configure the payment logger with the given handler.
|
|
|
|
Args:
|
|
handler: Handler for writing logs (FileHandler, StreamHandler, etc.)
|
|
formatter: Formatter for log output (optional)
|
|
level: Logging level (default INFO)
|
|
"""
|
|
_stdlib_payment_logger.setLevel(level)
|
|
|
|
if formatter:
|
|
handler.setFormatter(formatter)
|
|
|
|
_stdlib_payment_logger.addHandler(handler)
|
|
|
|
# Prevent duplication in parent loggers
|
|
_stdlib_payment_logger.propagate = False
|
|
|
|
|
|
def get_payment_logger() -> structlog.stdlib.BoundLogger:
|
|
"""Return the payment logger instance.
|
|
|
|
Alternative way to obtain the logger for modules that prefer
|
|
an explicit function call.
|
|
"""
|
|
return payment_logger
|