mosquito / aio-pika

AMQP 0.9 client designed for asyncio and humans.
https://aio-pika.readthedocs.org/
Apache License 2.0
1.25k stars 190 forks source link

Exceptions in the consume callback don't get to logs or stderr #257

Closed Kaveshnikov closed 4 years ago

Kaveshnikov commented 5 years ago

When exceptions acquire in the on_message() callback they don't go to logging even if set log level DEBUG at the aio_pika logger. Only if set DEBUG level at aiormq they become visible. I understand that it happens because of asyncio.Task machinery, but it is not obvious behavior. Could you add exceptions handle by default?

heckad commented 4 years ago

@Kaveshnikov, please, provide a simple example.

Kaveshnikov commented 4 years ago

Excuse me. Setting DEBUG level for aiormq will not help. The problem was in my logging settings. I used logging.config.dictConfig() which disables default loggers by default (sorry for the tautology). The only logger showing exception in my case is asyncio. A solution is to set disable_existing_loggers' to False in the logging config.

Code example ```python import logging.config import aio_pika from aiohttp import web async def on_message(_message: aio_pika.IncomingMessage): raise Exception('Something happen') async def consume(app: web.Application): connection: aio_pika.RobustConnection = app['mq'] channel = await connection.channel() await channel.set_qos(prefetch_count=1) queue: aio_pika.RobustQueue = await channel.declare_queue('hello', durable=False, auto_delete=True) await queue.consume(on_message) yield async def init_mq(app: web.Application) -> None: connection: aio_pika.RobustConnection = await aio_pika.connect() app['mq'] = connection yield await app['mq'].close() def create_app() -> web.Application: app: web.Application = web.Application() app.cleanup_ctx.append(init_mq) app.cleanup_ctx.append(consume) return app LOGGING_APP_LEVEL = 'DEBUG' if __name__ == '__main__': LOGGING: dict = { 'version': 1, 'disable_existing_loggers': False, 'formatters': { 'standard': { 'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s', }, }, 'handlers': { 'stderr': { 'level': 'DEBUG', 'class': 'logging.StreamHandler', 'formatter': 'standard', }, }, 'root': { 'handlers': ['stderr'], 'level': 'ERROR', }, 'loggers': { 'application': { 'handlers': ['stderr'], 'level': LOGGING_APP_LEVEL, 'propagate': True, }, 'aiohttp': { 'handlers': ['stderr'], 'level': LOGGING_APP_LEVEL, 'propagate': True, }, 'aio_pika': { 'handlers': ['stderr'], 'level': LOGGING_APP_LEVEL, 'propagate': True, }, 'aiormq': { 'handlers': ['stderr'], 'level': LOGGING_APP_LEVEL, 'propagate': True, }, }, } logging.config.dictConfig(LOGGING) app: web.Application = create_app() web.run_app(app, host='localhost', port=8080) ```
Kaveshnikov commented 4 years ago

This behavior is pretty expected, so I close the issue.