Files
remnawave-bedolaga-telegram…/app/utils/startup_timeline.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

184 lines
6.1 KiB
Python

import platform
import time
from collections.abc import Iterable, Sequence
from contextlib import asynccontextmanager
from dataclasses import dataclass
from typing import Any
@dataclass
class StepRecord:
title: str
icon: str
status_label: str
message: str
duration: float
class StageHandle:
def __init__(
self,
timeline: 'StartupTimeline',
title: str,
icon: str,
success_message: str | None,
) -> None:
self.timeline = timeline
self.title = title
self.icon = icon
self.message = success_message or ''
self.status_icon = ''
self.status_label = 'Готово'
self._explicit_status = False
def success(self, message: str | None = None) -> None:
if message is not None:
self.message = message
self.status_icon = ''
self.status_label = 'Готово'
self._explicit_status = True
def warning(self, message: str) -> None:
self.status_icon = '⚠️'
self.status_label = 'Предупреждение'
self.message = message
self._explicit_status = True
def skip(self, message: str) -> None:
self.status_icon = '⏭️'
self.status_label = 'Пропущено'
self.message = message
self._explicit_status = True
def failure(self, message: str) -> None:
self.status_icon = ''
self.status_label = 'Ошибка'
self.message = message
self._explicit_status = True
def log(self, message: str, icon: str = '') -> None:
self.timeline.logger.info('', icon=icon, message=message)
class StartupTimeline:
def __init__(self, logger: Any, app_name: str) -> None:
self.logger = logger
self.app_name = app_name
self.steps: list[StepRecord] = []
def _record_step(self, title: str, icon: str, status_label: str, message: str, duration: float) -> None:
self.steps.append(
StepRecord(
title=title,
icon=icon,
status_label=status_label,
message=message,
duration=duration,
)
)
def log_banner(self, metadata: Sequence[tuple[str, Any]] | None = None) -> None:
title_text = f'🚀 {self.app_name}'
subtitle_parts = [f'Python {platform.python_version()}']
if metadata:
for key, value in metadata:
subtitle_parts.append(f'{key}: {value}')
subtitle_text = ' | '.join(subtitle_parts)
width = max(len(title_text), len(subtitle_text))
border = '' + '' * (width + 2) + ''
self.logger.info(border)
self.logger.info('' + title_text.ljust(width) + '')
self.logger.info('' + subtitle_text.ljust(width) + '')
self.logger.info('' + '' * (width + 2) + '')
def log_section(self, title: str, lines: Iterable[str], icon: str = '📄') -> None:
items = [f'{icon} {title}'] + [f'{line}' for line in lines]
width = max(len(item) for item in items)
top = '' + '' * width + ''
middle = '' + '' * width + ''
bottom = '' + '' * width + ''
self.logger.info(top)
self.logger.info('' + items[0].ljust(width) + '')
self.logger.info(middle)
for item in items[1:]:
self.logger.info('' + item.ljust(width) + '')
self.logger.info(bottom)
def add_manual_step(
self,
title: str,
icon: str,
status_label: str,
message: str,
) -> None:
self.logger.info('', icon=icon, title=title)
self.logger.info('┗ —', icon=icon, title=title, status_label=status_label, message=message)
self._record_step(title, icon, status_label, message, 0.0)
@asynccontextmanager
async def stage(
self,
title: str,
icon: str = '⚙️',
description: str | None = None,
success_message: str | None = 'Готово',
):
if description:
self.logger.info('┏ —', icon=icon, title=title, description=description)
else:
self.logger.info('', icon=icon, title=title)
handle = StageHandle(self, title, icon, success_message)
start_time = time.perf_counter()
try:
yield handle
except Exception as exc:
message = str(exc)
handle.failure(message)
self.logger.exception('┣ ❌ — ошибка', title=title, message=message)
raise
finally:
duration = time.perf_counter() - start_time
if not handle._explicit_status:
handle.success(handle.message or 'Готово')
self.logger.info(
'┗ — [s]',
status_icon=handle.status_icon,
title=title,
message=handle.message,
duration=round(duration, 2),
)
self._record_step(
title=title,
icon=handle.status_icon,
status_label=handle.status_label,
message=handle.message,
duration=duration,
)
def log_summary(self) -> None:
if not self.steps:
return
lines = []
for step in self.steps:
base = f'{step.icon} {step.title}{step.status_label} [{step.duration:.2f}s]'
if step.message:
base += f' :: {step.message}'
lines.append(base)
width = max(len(line) for line in lines)
border_top = '' + '' * (width + 2) + ''
border_mid = '' + '' * (width + 2) + ''
border_bottom = '' + '' * (width + 2) + ''
title = 'РЕЗЮМЕ ЗАПУСКА'
self.logger.info(border_top)
self.logger.info('' + title.center(width) + '')
self.logger.info(border_mid)
for line in lines:
self.logger.info('' + line.ljust(width) + '')
self.logger.info(border_bottom)