Add logging for receiving updates

This commit is contained in:
Alex Root Junior 2019-11-27 01:26:24 +02:00
parent 334975bf21
commit 93a330c1f2
3 changed files with 33 additions and 1 deletions

View file

@ -1,8 +1,21 @@
from .api import methods, types from .api import methods, types
from .api.client import session from .api.client import session
from .api.client.bot import Bot from .api.client.bot import Bot
from .dispatcher import filters
from .dispatcher.dispatcher import Dispatcher
from .dispatcher.router import Router
__all__ = ("__api_version__", "__version__", "types", "methods", "Bot", "session") __all__ = (
"__api_version__",
"__version__",
"types",
"methods",
"Bot",
"session",
"Dispatcher",
"Router",
"filters",
)
__version__ = "3.0.0a0" __version__ = "3.0.0a0"
__api_version__ = "4.4" __api_version__ = "4.4"

View file

@ -1,5 +1,7 @@
import asyncio
from typing import AsyncGenerator, Optional from typing import AsyncGenerator, Optional
from .. import loggers
from ..api.client.bot import Bot from ..api.client.bot import Bot
from ..api.methods import TelegramMethod from ..api.methods import TelegramMethod
from ..api.types import Update from ..api.types import Update
@ -24,9 +26,23 @@ class Dispatcher(Router):
:param update: :param update:
:return: :return:
""" """
loop = asyncio.get_running_loop()
handled = False
start_time = loop.time()
Bot.set_current(bot) Bot.set_current(bot)
async for result in self.update_handler.trigger(update, bot=bot, **kwargs): async for result in self.update_handler.trigger(update, bot=bot, **kwargs):
yield result yield result
handled = True
finish_time = loop.time()
duration = (finish_time - start_time) * 1000
loggers.dispatcher.info(
"Update id=%s is %s. Duration %d ms.",
update.update_id,
"handled" if handled else "not handled",
duration,
)
@classmethod @classmethod
async def listen_updates(cls, bot: Bot) -> AsyncGenerator[Update, None]: async def listen_updates(cls, bot: Bot) -> AsyncGenerator[Update, None]:

3
aiogram/loggers.py Normal file
View file

@ -0,0 +1,3 @@
import logging
dispatcher = logging.getLogger("aiogram.dispatcher")