我在 Heroku 上运行了一个小型 Python Web 应用程序,并且我已经将日志排空到 loggly 上。当引发异常时,回溯在 loggly 中显示为单独的行。这当然很难搜索。
如何使回溯在 Loggly 上显示为单个日志?
例子:
您应该根据此页面中的说明设置 python 日志记录:
https://www.loggly.com/docs/python-http/
修改第 3 步(发送日志事件的位置),以便可以发送异常,如下所示:
import logging
import logging.config
import loggly.handlers
logging.config.fileConfig('python.conf')
logger = logging.getLogger('myLogger')
logger.info('Test log')
try:
main_loop()
except Exception:
logger.exception("Fatal error in main loop")
您将看到异常显示为单个日志事件:
{ "loggerName":"myLogger", "asciTime":"2015-08-04 15:09:00,220", "fileName":"test_log.py", "logRecordCreationTime":"1438726140.220768", "functionName":"<module>", "levelNo":"40", "lineNo":"15", "time":"220", "levelName":"ERROR", "message":"Fatal error in main loop"}
Traceback (most recent call last):
File "./test_log.py", line 13, in <module>
main_loop()
NameError: name 'main_loop' is not defined
}