Python logging: Why is __init__ called twice?

后端 未结 2 1610
予麋鹿
予麋鹿 2021-02-07 12:25

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 _

相关标签:
2条回答
  • 2021-02-07 12:53
    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 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.

    0 讨论(0)
  • 2021-02-07 13:17

    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.

    0 讨论(0)
提交回复
热议问题