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 _
- Why is init called twice?
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__'
:
#!/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")
2 . Why is del called less often than init?
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.