python stdout traceback作为loggly中的单独行

时间:2014-08-26 15:13:40

标签: python traceback loggly

我正在Heroku上运行一个小型python网络应用程序,而且我已经将日志耗尽了。引发异常时,回溯在loggly中显示为单独的行。这当然很难搜索。

如何将回溯显示为Loggly上的单个日志?

实施例: enter image description here

1 个答案:

答案 0 :(得分:0)

您应该根据此页面中的说明设置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
}