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?
I have done something similar in my phd project. I do the initialization in the
__init__.py
of themodule with basic config (see here):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):
where
config.get('default','logconf')
is the logging configuration file path. Then in any sub-module you use the regular: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 guardif __name__=='__main__':
or makefoo
ordaily_report
a module so that you can put theset_up
call on the__init__.py
file. Then you can use it as described above.You can see the documentation for more details.