TL;DR: I went deep into Python's logging module to understand how logging organizes loggers from different modules.

Key point: No matter how many modules my top-level script imports or calls, the logging engine (i.e. the code and logic behind the logging module) sets up a hierarchy of loggers where each module gets its own logger object, named after that module, and all of those loggers sit beneath the root logger in a clearly defined tree. With this architecture, I can easily see which module each message comes from.

For example, let's say my top-level code is:

import logging
from my_tools.gsheet_utils import df_from_gsheet
 
logger = logging.getLogger(__name__)

df = df_from_gsheet()

Step One:

  • Python runs import logging
  • Python creates a root logger with the name "" (empty string).
  • Logger structure is now
    root ("")

Step Two:

  • Python runs logger = logging.getLogger(__name__)
  • Python creates (or retrieves) a logger named "__main__", which represents the top-level script. This logger sends its messages upward to the root logger.
  • Logger structure is now
    root ("")
    ├── __main__

Step Three:

  • Python runs logger = logging.getLogger(__name__) inside df_from_gsheet.
  • Python creates (or retrieves) a logger named my_tools.gsheet_utils. It is a direct child of the root logger and a sibling to __main__.
  • Logger structure is now
    root ("")
    ├── __main__
    ├── my_tools.gsheet_utils

Golden Takeaway: This architecture lets me easily see where my code might be experiencing problems by creating a tidy hierarchy that cleanly separates messages by module.

Related Posts

tags: #Python, #logging, #tools-and-languages, #applied-learning, #general-programming

Understanding Python’s Logger Hierarchy