首页 文章

Python日志记录 . 使用formatter和logging.exception()

提问于
浏览
4

我有一个日志格式化程序,目前将所有常规日志记录级别格式化为json字符串 . 当我想查看日志时,我使用带有javascript的网页,将json转换为表中的s . 我使用的格式化程序是:

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>'
)

这很好用 . 我现在想使用logging.exception()记录异常 .

看来logging.exception不提供在所有其他日志记录级别中找到的项目的字典 .

如何捕获logging.exception提供的堆栈跟踪,以便将其转换为与我为所有其他类型的日志条目创建的json兼容的表单?我需要的是,对于logging.exception,抓取stacktrace并将其放入json的“msg”元素中 . 如果logging.exception没有提供其他dict元素,如asctime,level等,我可以自动创建它们 .

1 回答

相关问题