日志处理程序如何变成 str?

发布于 2024-09-29 10:14:37 字数 2140 浏览 3 评论 0原文

我有一个记录器,它在脚本开始时正常运行,然后在中间中断。看起来它的处理程序被 str 覆盖,但我不知道在哪里。

在脚本开始时,我打印处理程序及其级别。以下代码:

print 'Array of handlers', logger.handlers
    for h in logger.handlers:
        print 'Handler', h
        print 'Handler level', h.level

产生以下结果:

Array of handlers [<logging.FileHandler instance at 0x19ef320>]
Handler <logging.FileHandler instance at 0x19ef320>
Handler level 0

现在在执行过程中,您将看到记录器的处理程序 (hdlr) 被解释为 str

Started from <class 'mymodule.ext.freebase.HTTPMetawebSession'>.
Traceback (most recent call last):
  File "hadoop/get_web_data.py", line 144, in <module>
    main()
  File "hadoop/get_web_data.py", line 121, in main
    for count, performer in enumerate(results):
  File "/home/wraith/dev/modules/mymodule/ext/freebase.py", line 126, in mqlreaditer
    r = self._httpreq_json(service, 'POST', form=dict(query=qstr))
  File "/home/wraith/dev/modules/mymodule/contrib/freebase/api/session.py", line 369, in _httpreq_json
    resp, body = self._httpreq(*args, **kws)
  File "/home/wraith/dev/modules/mymodule/contrib/freebase/api/session.py", line 346, in _httpreq
    self.log.info('%s %s%s%s', method, url, formstr, headerstr)
  File "/usr/lib/python2.5/logging/__init__.py", line 985, in info
    apply(self._log, (INFO, msg, args), kwargs)
  File "/usr/lib/python2.5/logging/__init__.py", line 1101, in _log
    self.handle(record)
  File "/usr/lib/python2.5/logging/__init__.py", line 1111, in handle
    self.callHandlers(record)
  File "/usr/lib/python2.5/logging/__init__.py", line 1147, in callHandlers
    if record.levelno >= hdlr.level:
AttributeError: 'str' object has no attribute 'level'

在最后两行中,hdlr.level 爆炸,因为 hdlr 不是 str

if record.levelno >= hdlr.level:
AttributeError: 'str' object has no attribute 'level'

在开始设置处理程序后(这很好),我不会添加另一个处理程序或以任何方式更改现有处理程序。我在 logger 上调用的唯一命令是 logger.info('event to log')

什么会以这种方式改变记录器的处理程序

I have a logger that functions properly at the start of a script, then breaks in the middle. It looks like its handler is getting overwritten by a str, but I can't figure out where.

At the start of the script, I'm printing the handler and its level. The following code:

print 'Array of handlers', logger.handlers
    for h in logger.handlers:
        print 'Handler', h
        print 'Handler level', h.level

produces this:

Array of handlers [<logging.FileHandler instance at 0x19ef320>]
Handler <logging.FileHandler instance at 0x19ef320>
Handler level 0

Now in the middle of execution, you'll see that the logger's handler (hdlr) is interpreted as a str.

Started from <class 'mymodule.ext.freebase.HTTPMetawebSession'>.
Traceback (most recent call last):
  File "hadoop/get_web_data.py", line 144, in <module>
    main()
  File "hadoop/get_web_data.py", line 121, in main
    for count, performer in enumerate(results):
  File "/home/wraith/dev/modules/mymodule/ext/freebase.py", line 126, in mqlreaditer
    r = self._httpreq_json(service, 'POST', form=dict(query=qstr))
  File "/home/wraith/dev/modules/mymodule/contrib/freebase/api/session.py", line 369, in _httpreq_json
    resp, body = self._httpreq(*args, **kws)
  File "/home/wraith/dev/modules/mymodule/contrib/freebase/api/session.py", line 346, in _httpreq
    self.log.info('%s %s%s%s', method, url, formstr, headerstr)
  File "/usr/lib/python2.5/logging/__init__.py", line 985, in info
    apply(self._log, (INFO, msg, args), kwargs)
  File "/usr/lib/python2.5/logging/__init__.py", line 1101, in _log
    self.handle(record)
  File "/usr/lib/python2.5/logging/__init__.py", line 1111, in handle
    self.callHandlers(record)
  File "/usr/lib/python2.5/logging/__init__.py", line 1147, in callHandlers
    if record.levelno >= hdlr.level:
AttributeError: 'str' object has no attribute 'level'

In the last 2 lines, hdlr.level blows up because hdlr is not a str.

if record.levelno >= hdlr.level:
AttributeError: 'str' object has no attribute 'level'

After setting the handler at the beginning, which is fine, I do not add another handler or alter the existing in any way. The only command I call on logger is logger.info('event to log').

What would alter the logger's handler in this way?

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

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

发布评论

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

评论(1

泡沫很甜 2024-10-06 10:14:37

该字符串的内容可能会为您提供有关出错的方式和原因的线索。

我建议

print(repr(self.log.handlers))

在调用 info 之前放置:

self.log.info('%s %s%s%s', method, url, formstr, headerstr)

The contents of the string might give you a clue as to how and why it is going wrong.

I suggest putting

print(repr(self.log.handlers))

right before your call to info:

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