I am learning logging with the standard Python library documentation and it seems I am missing something trivial. I want to have logging in multiple modules but configure each instance only in the main file. I think that what I am doing wrong is that basicConfig only works for the functions that control the root logger, but I am confused.
Could you point me in the right direction? Thanks in advance.
# main.py import logging import module_log logging.basicConfig(level=logging.WARNING) logger = logging.getLogger(__name__) def test1(): logger.warning("This one is not showing in log but that is what I expect") module_log.test2() if __name__ == "__main__": test1() # module.py import logging logger = logging.getLogger(__name__) def test2(): logger.warning("This one shouldn't be showing in log but it is") EDIT 1: Updated the code to better illustrate the problem.
EDIT 2: I think I found the source of the problem, even thought I don't fully understand why. I was not executing the main.py directly, I was calling it from within the Maya(3d software) interpreter (but the same happens with the Python interpreter) If you run:
>>> import main_log >>> main_log.test1() It works! But then I changed main_log.py to change the debug level. I am used to run "reload()" when updating modules but with this specific module it seems not to be working so it kept showing the same results and that why I was getting always the same results no matter what the debug level was.
>>> reload(main_log) >>> main_log.test1() Until this point, my only workaround is shutting down Maya completely and re-opening it (guess the same happens within the python interpreter). I will appreciate any further help!
main.pyandmodule.pyas shown, I cannot reproduce the behavior you describe; the warning from each is only displayed iflogging.basicConfigsets the logging level tologging.WARNINGor lower.