Friday, 4 October 2019

Python logger hierarchy and the root logger when logging with multiple modules

I have this setup:

main.py
/module
/module/__init__.py (empty)
/module.py

And here is the code for my two files, main.py and module.py respectively:

main.py

import logging
from module import module

logger = logging.getLogger(__name__)

def test():
    logger.warning('in main.py/test')

def main():
    handler = logging.StreamHandler()
    handler.setLevel(logging.INFO)
    formatter = logging.Formatter('%(asctime)s %(name)s/%(module)s [%(levelname)s]: %(message)s', '%Y-%m-%d %H:%M:%S')
    handler.setFormatter(formatter)
    logger.addHandler(handler)

    logger.warning('in main.py/main')
    module.something()

if __name__ == "__main__":
    main()    

module.py

import logging
logger = logging.getLogger(__name__)

def something():
    logger.warning('in module.py/something')

So, what I noticed is that this outputs the following (notice how the module logger has no formatting):

2019-10-01 09:03:40 __main__/main [WARNING]: in main.py/main
in module.py/something

It only seems like only after I make an edit in main.py to change logger = logging.getLogger( __ name __ ) to logger = logging.getLogger() or add logger = logging.getLogger() after def main(): that it logs like this (which is what I want):

2019-10-01 09:04:13 root/main [WARNING]: in main.py/main
2019-10-01 09:04:13 module.module/module [WARNING]: in module.py/something

Why is that? I thought that because main.py is importing module.py, it is naturally higher on the hierarchical scale so module.py would inherit the logger settings as defined in main.py. Do need to explicitly set the root logger (with logger = logging.getLogger()) in main for the inheritance to work? Did I not configure my folder structure correctly to make module.py's logger inherit main.py's logger settings, or is folder structure irrelevant?

The reason I ask is because I thought one should use logger = logging.getLogger( __ name __ ) throughout (even in main.py) and then based on the import structure (or folder structure?), that would determine the hierarchy and loggers would inherit accordingly. And the reason I was making that assumption is because what if I was importing main.py into another program? I guess my point is, I want to make logging as generic as possible such that I can import one module into another and it always inherits the parent's logger settings. Is there a way to display the underlying hierarchy of all the modules for debugging/learning purposes?



from Python logger hierarchy and the root logger when logging with multiple modules

No comments:

Post a Comment