1

我有一个记录器,它在脚本开始时正常运行,然后在中间中断。看起来它的处理程序被 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.info('event to log').

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

4

1 回答 1

0

字符串的内容可能会为您提供有关它如何以及为什么出错的线索。

我建议把

print(repr(self.log.handlers))

在您致电之前info

self.log.info('%s %s%s%s', method, url, formstr, headerstr)
于 2010-10-28T01:50:29.307 回答