Próbuję użyć logowania Pythona z pliku konfiguracyjnego i własny moduł obsługi. Działa to w pewnym stopniu. To, co mnie naprawdę łamie, to __init__
, które jest wywoływane dwa razy, a __del__
jest wywoływane raz. Po usunięciu całego pliku konfiguracyjnego i utworzeniu modułu obsługi bezpośrednio w kodzie __init__
jest wywoływana raz, a __del__
nigdy nie jest wywoływana.Logowanie w języku Python: Dlaczego __init__ wywoływane jest dwa razy?
Moje pytania:
- Dlaczego
__init__
wywołana dwukrotnie? - Dlaczego nazwa
__del__
jest wywoływana rzadziej niż__init__
?
Kod:
#!/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")
Plik konfiguracyjny:
[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
wyjściowe wygląda następująco:
init called
init called
del called
Korzystanie debuggera aby uzyskać ślad stosu jak sugeruje Sentinal ujawnia to:
pierwsze wezwanie:
> /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
drugie wezwanie:
> /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"
"" "Dlaczego init jest wywoływany dwa razy?" "" - weź debugger i sprawdź, dlaczego jest wywoływany dwa razy. Przeglądanie stosu wywołań może być pomocne ... –