分别记录异常的每一行

2024-09-26 17:55:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在登录到syslog(在通往papertrail的路上),我希望在最终的日志中可以看到异常。你知道吗

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

Sep 02 08:35:16 web1.stage.releng.webapp.scl3.mozilla.com relengapi: Exception on /tokenauth/tokens [POST]#012Traceback (most recent call last):#012  File "/data/www/relengapi/virtualenv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request#012    rv = self.dispatch_request()#012  File "/data/www/relengapi/virtualenv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request#012    return self.view_functions[rule.endpoint](**req.view_args)#012

也就是说,由#012分隔的一条很长的线。事实上,文书记录在完成之前就把它切断了。你知道吗

我想找到一种方法,将该异常拆分为多行。我愿意monkeypatch或子类日志模块这样做,但我找不到一个好地方。你知道吗

这里的最佳做法是什么?你知道吗


Tags: inpyappflaskdatavirtualenvrequestlib
1条回答
网友
1楼 · 发布于 2024-09-26 17:55:07

以下似乎起到了作用:

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。有更好的办法吗?你知道吗

相关问题 更多 >

    热门问题