uli*_*i42 13 python logging config init handler
我试图使用配置文件和自己的处理程序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")
Run Code Online (Sandbox Code Playgroud)
配置文件:
[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
Run Code Online (Sandbox Code Playgroud)
输出如下:
init called
init called
del called
Run Code Online (Sandbox Code Playgroud)
使用调试器获取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
Run Code Online (Sandbox Code Playgroud)
第二个电话:
> /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"
Run Code Online (Sandbox Code Playgroud)
Céd*_*ien 12
- 为什么init被调用两次?
如果您遵循logging模块的代码,您将看到在加载日志记录配置文件时,它会实例化所有处理程序(First instantiation).
在你的代码中,你声明你的处理程序test1.Test1TimedRotatingFileHandler,所以当它尝试导入你的处理程序时,它解析test1模块中的代码...所以它重新创建处理程序!
更正后的代码将使用__name__ == '__main__':
#!/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)
if __name__ == "__main__":
logging.config.fileConfig('./test1.conf')
logger = logging.getLogger("test1")
Run Code Online (Sandbox Code Playgroud)
2.为什么del比init更少被调用?
通常,在__del__-python-want时调用运算符,更准确地说,当垃圾收集器决定对对象进行垃圾收集时调用它.这不一定就在你发布之后.
您缺少对if __name__ == "__main__":日志记录配置代码的警惕.在logging导入test1模块以查找类引用时,它将再次执行.
或者,使用__main__.Test1TimedRotatingFileHandler配置文件中的名称,或者将配置代码和处理程序类放在不同的文件中.
| 归档时间: |
|
| 查看次数: |
5080 次 |
| 最近记录: |