Logging in a Framework

2019-06-20 18:51发布

Imagine there is a framework which provides a method called logutils.set_up() which sets up the logging according to some config.

Setting up the logging should be done as early as possible since warnings emitted during importing libraries should not be lost.

Since the old way (if __name__=='__main__':) looks ugly, we use console_script entrypoints to register the main() method.

# foo/daily_report.py
from framework import logutils
logutils.set_up()
def main():
    ...

My problem is that logutils.set_up() might be called twice:

Imagine there is a second console script which calls logutils.set_up() and imports daily_report.py.

I can change the framework code and set_up() to do nothing in the second call to logutils.set_up(), but this feels clumsy. I would like to avoid it.

How can I be sure that logutils.set_up() gets only executed once?

7条回答
不美不萌又怎样
2楼-- · 2019-06-20 19:50

I have done something similar in my phd project. I do the initialization in the __init__.py of themodule with basic config (see here):

logging.getLogger('modulename').addHandler(logging.NullHandler())
FORMAT = '%(name)s:%(levelname)s:  %(message)s'
logging.basicConfig(format=FORMAT)

And then later, for instance if a config file is provided, you overwrite the config. As an example (you can find this in the constructor of EvoDevoWorkbench):

logging.config.fileConfig(config.get('default','logconf'),
                          disable_existing_loggers=False)

where config.get('default','logconf') is the logging configuration file path. Then in any sub-module you use the regular:

log = logging.getLogger(__name__)

In your specific case, if you setup the logging (or call the set_up) inside framework's __init__.py then it will never be called twice. If you cannot do this, the only way I see is to either use the guard if __name__=='__main__': or make foo or daily_report a module so that you can put the set_up call on the __init__.py file. Then you can use it as described above.

You can see the documentation for more details.

查看更多
登录 后发表回答