I have a log formatter that currently formats all conventional logging levels into a json string. When I want to view the log I use a web page with javascript that transforms the json into 's in a in a table. The formatter I use is:
formatter = logging.Formatter(
'{"datetime":"%(asctime)s","level":"%(levelname)s","app":"%(app_name)s","module":"%(module)s",' +
'"line":"%(lineno)d","function":"%(funcName)s","msg":"%(msg)s"}' #<switch2>'
)
This works fine. I now want to log exceptions using logging.exception().
It appears that logging.exception does not provide the dict of items that is found in all the other logging levels.
How can I capture the stack trace that logging.exception supplies so I can transform it into a form that is compatible with the json that I create for all the other kinds of log entries? What I need is, for logging.exception's, to grab the stacktrace and put it into the "msg" element of the json. If logging.exception does not provide the other dict elements like asctime, level, etc I can create them myself on the fly.
Looks like this is related to the logging.exception doesn't accept 'extra' bug (which was fixed in the 2.7 and 3.2 branches).
As a workaround, use logging.error()
passing exc_info=True
argument.
FYI, logging.exception()
actually does call logging.error()
under the hood, quote from cpython 2.7 source:
def exception(self, msg, *args, **kwargs):
"""
Convenience method for logging an ERROR with exception information.
"""
kwargs['exc_info'] = True
self.error(msg, *args, **kwargs)
See also:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With