How to use logging.exception with a level of INFO or WARNING

logging.Logger.exception prints a traceback and logs the passed message with a level of ERROR.
How can I have the same behavior but have it be passed with a level of WARNING or something else ?

The source code of Python’s stdlib is usually quite readable. You can see here the implementation of Logger.exception:

    def exception(self, msg, *args, exc_info=True, **kwargs):
        """
        Convenience method for logging an ERROR with exception information.
        """
        self.error(msg, *args, exc_info=exc_info, **kwargs)

As you can see, it just calls error() with the keyword parameter exc_info=True. What you want is logger.warning("message", exc_info=True).

1 Like