Files
remnawave-bedolaga-telegram…/app/middlewares/logging.py
c0mrade 9a2aea038a chore: add uv package manager and ruff linter configuration
- Add pyproject.toml with uv and ruff configuration
- Pin Python version to 3.13 via .python-version
- Add Makefile commands: lint, format, fix
- Apply ruff formatting to entire codebase
- Remove unused imports (base64 in yookassa/simple_subscription)
- Update .gitignore for new config files
2026-01-24 17:45:27 +03:00

44 lines
1.5 KiB
Python

import logging
import time
from collections.abc import Awaitable, Callable
from typing import Any
from aiogram import BaseMiddleware
from aiogram.types import CallbackQuery, Message, TelegramObject
logger = logging.getLogger(__name__)
class LoggingMiddleware(BaseMiddleware):
async def __call__(
self,
handler: Callable[[TelegramObject, dict[str, Any]], Awaitable[Any]],
event: TelegramObject,
data: dict[str, Any],
) -> Any:
start_time = time.time()
try:
if isinstance(event, Message):
user_info = f'@{event.from_user.username}' if event.from_user.username else f'ID:{event.from_user.id}'
text = event.text or event.caption or '[медиа]'
logger.info(f'📩 Сообщение от {user_info}: {text}')
elif isinstance(event, CallbackQuery):
user_info = f'@{event.from_user.username}' if event.from_user.username else f'ID:{event.from_user.id}'
logger.info(f'🔘 Callback от {user_info}: {event.data}')
result = await handler(event, data)
execution_time = time.time() - start_time
if execution_time > 1.0:
logger.warning(f'⏱️ Медленная операция: {execution_time:.2f}s')
return result
except Exception as e:
execution_time = time.time() - start_time
logger.error(f'❌ Ошибка при обработке события за {execution_time:.2f}s: {e}')
raise