设置delay参数时RotatingFileHandler抛出异常

发布于 2024-08-25 17:01:36 字数 1552 浏览 7 评论 0原文

当我在 Python 2.6 下运行以下代码时

import logging
from logging.handlers import RotatingFileHandler

rfh = RotatingFileHandler("testing.log", delay=True)
logging.getLogger().addHandler(rfh)
logging.warning("Boo!")

,最后一行抛出 AttributeError: RotatingFileHandler instance has no attribute 'level'。 添加了这一行

rfh.setLevel(logging.DEBUG)

因此,我在调用 addHandler 之前 ,然后最后一行抛出 AttributeError: RotatingFileHandler instance has no attribute 'filters'。因此,如果我手动将过滤器设置为空列表,那么它会抱怨没有属性 lock 等。

当我删除 delay=True 将其保留为默认值为 False 如此处记录,问题完全消失。

我错过了什么吗?如何正确使用 RotatingFileHandler 类的 delay 参数?

编辑:经过进一步分析(在下面我自己的答案中介绍),这看起来像一个错误,但我在 Python bug 跟踪器,甚至尝试不同的搜索词,所以我想我会报告它。

但是,如果有人可以找到实际的错误报告,那么我就可以避免提交重复的报告并浪费 Python 开发人员的时间。我将推迟几个小时报告错误,如果有人发布包含当前错误报告的答案,那么我将接受此问题的答案。

When I run the following code under Python 2.6

import logging
from logging.handlers import RotatingFileHandler

rfh = RotatingFileHandler("testing.log", delay=True)
logging.getLogger().addHandler(rfh)
logging.warning("Boo!")

then the last line throws AttributeError: RotatingFileHandler instance has no attribute 'level'. So I add the line

rfh.setLevel(logging.DEBUG)

before the call to addHandler, and then the last line throws AttributeError: RotatingFileHandler instance has no attribute 'filters'. So if I manually set filters to be an empty list, then it complains about not having the attribute lock, etc.

When I remove the delay=True to leave it as the default value of False as documented here, the problem completely goes away.

Am I missing something? How do I properly use the delay parameter of the RotatingFileHandler class?

EDIT: Upon further analysis (presented in my own answer below), this looks like a bug, but I can't find a bug report on this in the Python bug tracker, even trying different search terms, so I guess I'll report it.

However, if someone can locate the actual bug report, then I can avoid submitting a duplicate reporting and wasting the time of the Python developers. I'll hold off on reporting the bug for a few hours, and if someone posts an answer that has the current bug report, then I'll accept that answer for this question.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

長街聽風 2024-09-01 17:01:36

我调查了这个问题:它已在 2009 年 1 月 20 日的 Python SVN r68829 中修复。这是在 2.6.1 发布之后但在 2.6.2 发布之前。

请升级到Python 2.6.2或更高版本。

我已更新您提交的错误。顺便说一句,提交的原始错误报告是 #5013,您可以通过搜索所有问题(而不仅仅是打开的)用于 RotatingFileHandler,例如 (来自 此页)。

I've investigated this issue: it was fixed in Python SVN r68829 dated 20 Jan, 2009. This was after the release of 2.6.1 but before the release of 2.6.2.

Please upgrade to Python 2.6.2, or a later version.

I've updated the bug you filed. BTW the original bug report filed was #5013, which you could have found by searching all issues (not just open ones) for RotatingFileHandler, like this (from this page).

不再让梦枯萎 2024-09-01 17:01:36

我想我刚刚弄清楚了这一点:

所以看起来当设置延迟时,类层次结构中某处的父 __init__ 方法不会被调用。事实上,在我的 Python 安装中检查文件 logging/__init__.py 的源代码时,我在 FileHandler.__init__ 方法中看到以下代码

if delay:
    self.stream = None
else:
    stream = self._open()
    StreamHandler.__init__(self, stream)

: code>FileHandler.emit 方法检查未打开的流并在执行日志记录时完成初始化:

if self.stream is None:
    stream = self._open()
    StreamHandler.__init__(self, stream)
StreamHandler.emit(self, record)

所以问题是在 BaseRotatingHandler.emit 方法中,shouldRollover code> 和 doRollover 方法在 emit 记录之前调用。这会导致调用一些方法,这些方法本身假定 __init__ 进程已完成。

这看起来像一个错误,所以如果我找不到它已经被报告过,我会报告它。

I think I've just figured this out:

So it looks like one of the parent __init__ methods somewhere up the class hierarchy isn't getting called when delay is set. Indeed, examining the source code to the file logging/__init__.py in my Python install, I see the following code in the FileHandler.__init__ method:

if delay:
    self.stream = None
else:
    stream = self._open()
    StreamHandler.__init__(self, stream)

It looks like the FileHandler.emit method checks for un-opened streams and finishes initialization when logging is performed:

if self.stream is None:
    stream = self._open()
    StreamHandler.__init__(self, stream)
StreamHandler.emit(self, record)

So the problem is that in the BaseRotatingHandler.emit method, the shouldRollover and doRollover methods are called before emit-ing the record. This causes methods to be called which themselves assumed that the __init__ process has completed.

This looks like a bug, so I'll report it as such if I can't find it having been already been reported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文