Skip to content Skip to sidebar Skip to footer

Python Logging: Why Is __init__ Called Twice?

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 on

Solution 1:

  1. 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 pythonimport logging
import logging.handlers
import logging.config

classTest1TimedRotatingFileHandler(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"ifhasattr(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.

Solution 2:

You are missing an if __name__ == "__main__": guard around your logging configuration code. It is getting executed a second time when logging imports your test1 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.

Post a Comment for "Python Logging: Why Is __init__ Called Twice?"