Python 日志记录:为什么 __init__ 被调用两次?
我正在尝试将 python 日志记录与配置文件和自己的处理程序一起使用。这在某种程度上是有效的。真正让我困惑的是 __init__
被调用了两次而 __del__
被调用了一次。当我删除整个配置文件内容并直接在代码中创建处理程序时, __init__
被调用一次,并且 __del__
永远不会被调用。
我的问题:
- 为什么
__init__
被调用两次? - 为什么
__del__
的调用频率低于__init__
?
代码:
#!/bin/env python
import logging
import logging.handlers
import logging.config
class Test1TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
def __init__(self,filename):
print "init called"
logging.handlers.TimedRotatingFileHandler.__init__(self,filename, when='S', interval=86400, backupCount=8, encoding=None)
def __del__(self):
print "del called"
if hasattr(logging.handlers.TimedRotatingFileHandler,"__del__"):
logging.handlers.TimedRotatingFileHandler.__del__(self)
logging.config.fileConfig('/root/test1.conf')
logger = logging.getLogger("test1")
配置文件:
[formatters]
keys: simple
[handlers]
keys: file
[loggers]
keys: root
[formatter_simple]
format: "%(message)s"
[handler_file]
class: test1.Test1TimedRotatingFileHandler
args: ("/root/test1.log",)
level=INFO
[logger_root]
level: INFO
handlers: file
qualname: test1
输出如下所示:
init called
init called
del called
按照 Sentinal 的建议使用调试器获取堆栈跟踪显示:
第一次调用:
> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) where
/root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(156)_install_handlers()
-> klass = _resolve(klass)
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(94)_resolve()
-> found = __import__(used)
/root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(159)_install_handlers()
-> h = klass(*args)
> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) c
init called
第二次调用:
> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) w
/root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(159)_install_handlers()
-> h = klass(*args)
> /root/test1.py(12)__init__()
-> print "init called"
I am trying to use python logging with a config file and an own handler. This works to some degree. What really puzzle me is __init__
being called twice and __del__
being called once. When I remove the whole config file stuff and create the handler directly within the code __init__
is called once and __del__
is never called.
My questions:
- Why is
__init__
called twice? - Why is
__del__
called less often than__init__
?
The code:
#!/bin/env python
import logging
import logging.handlers
import logging.config
class Test1TimedRotatingFileHandler(logging.handlers.TimedRotatingFileHandler):
def __init__(self,filename):
print "init called"
logging.handlers.TimedRotatingFileHandler.__init__(self,filename, when='S', interval=86400, backupCount=8, encoding=None)
def __del__(self):
print "del called"
if hasattr(logging.handlers.TimedRotatingFileHandler,"__del__"):
logging.handlers.TimedRotatingFileHandler.__del__(self)
logging.config.fileConfig('/root/test1.conf')
logger = logging.getLogger("test1")
The config file:
[formatters]
keys: simple
[handlers]
keys: file
[loggers]
keys: root
[formatter_simple]
format: "%(message)s"
[handler_file]
class: test1.Test1TimedRotatingFileHandler
args: ("/root/test1.log",)
level=INFO
[logger_root]
level: INFO
handlers: file
qualname: test1
Output looks like this:
init called
init called
del called
Using the debugger to get the stack trace as suggested by Sentinal reveals this:
First call:
> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) where
/root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(156)_install_handlers()
-> klass = _resolve(klass)
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(94)_resolve()
-> found = __import__(used)
/root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(159)_install_handlers()
-> h = klass(*args)
> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) c
init called
Second call:
> /root/test1.py(12)__init__()
-> print "init called"
(Pdb) w
/root/test1.py(21)<module>()
-> logging.config.fileConfig('/root/test1.conf')
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(84)fileConfig()
-> handlers = _install_handlers(cp, formatters)
/usr/local/python/2.6.4/lib/python2.6/logging/config.py(159)_install_handlers()
-> h = klass(*args)
> /root/test1.py(12)__init__()
-> print "init called"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您遵循
logging
模块的代码,您会发现当您加载日志配置文件时,它会实例化所有处理程序(第一次实例化)。在您的代码中,您可以像
test1.Test1TimedRotatingFileHandler
一样声明您的处理程序,因此当它尝试导入您的处理程序时,它会解析 test1 模块中的代码...因此它会重新创建处理程序!更正后的代码将使用
__name__ == '__main__'
进行保护:一般来说,
__del__
操作符在python想要的时候被调用,更准确地说,是在垃圾收集器决定对对象进行垃圾收集时调用;这不一定是在您释放它之后。If you follow the code of the
logging
module, you'll see that when you're loading the logging configuration file, it instantiates all the handlers (First instantiation).In your code, you declare your handler like
test1.Test1TimedRotatingFileHandler
, so when it try to import your handler, it parses the code in the test1 module... so it recreates the handler !!Corrected code will guard using
__name__ == '__main__'
:In general, the
__del__
operator is called when-python-wants, more exactly, it is called when the garbage collector decides to garbage-collect the object; this is not necessarily just after you release it.您在日志记录配置代码周围缺少
if __name__ == "__main__":
保护。当logging
导入test1
模块以查找类引用时,它会第二次执行。或者,在配置文件中使用名称
__main__.Test1TimedRotatingFileHandler
,或者将配置代码和处理程序类放在不同的文件中。You are missing an
if __name__ == "__main__":
guard around your logging configuration code. It is getting executed a second time whenlogging
imports yourtest1
module to find the class reference.Alternatively, use the name
__main__.Test1TimedRotatingFileHandler
in the config file, or else put the configuration code and the handler class in different files.