分别记录异常的每一行

时间:2015-09-02 17:06:29

标签: python logging syslog

我正在登录到syslog(在前往papertrail的路上),并且我希望在最终日志中看到异常。

使用vanilla配置,异常如下所示:

>>>pyspark
I 17:51:00.209 NotebookApp] Serving notebooks from local directory: /Users/Thibault/code/kaggle
[I 17:51:00.209 NotebookApp] 0 active kernels
[I 17:51:00.210 NotebookApp] The IPython Notebook is running at: http://localhost:42424/
[I 17:51:00.210 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[I 17:51:11.980 NotebookApp] Kernel started: 53ad11b1-4fa4-459d-804c-0487036b0f29
15/09/02 17:51:15 WARN NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable

也就是说,由#012划分的很长的一行。实际上,papertrail会在它完成之前切断它。

我想找到一种方法将该异常分成多行。我愿意将monkeypatch或子类记录模块这样做,但我找不到一个好地方。

这里的最佳做法是什么?

1 个答案:

答案 0 :(得分:0)

以下似乎正在起作用:

old_log = logging.Logger._log
def new_log(self, level, msg, args, exc_info=None, extra=None):
    old_log(self, level, msg, args, exc_info=None, extra=extra)
    if exc_info:
        if not isinstance(exc_info, tuple):
            exc_info = sys.exc_info()
        exc_text = logging._defaultFormatter.formatException(exc_info)
        for line in exc_text.split("\n"):
            old_log(self, level, line, ())
logging.Logger._log = new_log

但我不喜欢使用logging._defaultFormatter。还有更好的方法吗?

相关问题