How do I do logging.exception() on exceptions gathered by asyncio.gather()?
Question:
I greatly enjoy the information returned by logging.exception()
, however this function can only be used in an exception handler.
Now, running asyncio.gather(..., return_exceptions=True)
does not raise exceptions; rather, the exceptions are returned as Exception
objects.
I would like to log these Exceptions
objects with the same details as with logging.exception()
, but since I’m not in an exception handler, how do I do that?
Answers:
You can take the exception instances returned by gather
and pass them as exc_info
like
results = asyncio.gather(*coros, return_exceptions=True)
for exc in [r for r in results if isinstance(r, BaseException)]
logger.debug("message", exc_info=exc)
This should give the same output as logger.exception("message")
.
results = asyncio.gather(*coros, return_exceptions=True)
for exc in [r.exception() for r in results]:
if r:
logger.error("message", exc_info=True)
You can iterate results returned by gather and log errors with exc_info
results = await asyncio.gather(*batch, return_exceptions=True)
for res in results:
if isinstance(res, BaseException):
logger.exception('Task exception', exc_info=res)
I greatly enjoy the information returned by logging.exception()
, however this function can only be used in an exception handler.
Now, running asyncio.gather(..., return_exceptions=True)
does not raise exceptions; rather, the exceptions are returned as Exception
objects.
I would like to log these Exceptions
objects with the same details as with logging.exception()
, but since I’m not in an exception handler, how do I do that?
You can take the exception instances returned by gather
and pass them as exc_info
like
results = asyncio.gather(*coros, return_exceptions=True)
for exc in [r for r in results if isinstance(r, BaseException)]
logger.debug("message", exc_info=exc)
This should give the same output as logger.exception("message")
.
results = asyncio.gather(*coros, return_exceptions=True)
for exc in [r.exception() for r in results]:
if r:
logger.error("message", exc_info=True)
You can iterate results returned by gather and log errors with exc_info
results = await asyncio.gather(*batch, return_exceptions=True)
for res in results:
if isinstance(res, BaseException):
logger.exception('Task exception', exc_info=res)