我正在使用structlog进行日志记录,并希望以json格式打印异常/堆栈跟踪。
目前它没有格式化,而是以字符串格式输出,这不太易读。
{
"message": "Error info with an exc",
"timestamp": "2022-03-31T13:32:33.928188+00:00",
"logger": "__main__",
"level": "error",
"exception": "Traceback (most recent call last):\n File \"../../main.py\", line 21, in <module>\n assert 'foo' == 'bar'\nAssertionError"
}
希望以json格式返回异常信息,例如:
{
"message": "Error info with an exc",
"timestamp": "2022-03-31T13:32:33.928188+00:00",
"logger": "__main__",
"level": "error",
"exception": {
"File": "../.../main.py",
"line": 21,
"function": "<module>",
"errorStatement": "assert 'foo' == 'bar'",
"errorType":"AssertionError",
}
}
这只是一个小例子,我也使用了traceback库并传递了stackTrace,它被打印成一个大字符串块。
我们是否有可用的库来执行stacktrace json格式化?还是我们需要编写自定义函数?
exception.split('\n')
?) - Evgenii