How to pickle loggers?

后端 未结 5 1951
粉色の甜心
粉色の甜心 2021-02-14 22:17

Working on a project that requires that I am able to pickle the container object at any point, since we expect it to fail on external conditions quite frequently and be able to

5条回答
  •  春和景丽
    2021-02-14 22:44

    You could also create a class that implements a property which returns the needed logger. Every class which inherits from this "LoggerMixin" is now able to use the logger in the same way you were using it before.

    class LoggerMixin():
        @property
        def logger(self):
            component = "{}.{}".format(type(self).__module__, type(self).__name__)
            return logging.getLogger(component)
    
    class Foo(LoggerMixin):
        def __init__(self):
            self.logger.info("initialize class")
    
        def bar(self):
            self.logger.info("execute bar")
    

提交回复
热议问题