Python Logging exc_info By Default

Viewed 1104

I am looking for a way to have logging.info, logging.warning, logging.error, etc. set exc_info=True by default. Refactoring the project to include this parameter isn't feasible at this point, so I'm looking for a way to set this for the logging module at a project-wide scope.

I am specifically looking for a way to set this parameter. Workarounds to retrieve the exception like sys.exc_info or traceback.format_exc() are not what I'm looking for.

Additionally, please let me know if this just isn't possible.

1 Answers

Posting this answer in case anyone has a similar problem - taken from this question.

The easiest way I've found is to use functools.partial at the start of your project. This allows you to change the default parameter value, while still being able to set it to False for specific function calls if you want to!

import logging
from functools import *

logging.info = partial(logging.info, exc_info=True)
Related