Search⌘ K
AI Features

How to log From Multiple Modules (and Formatting too!)

Explore how to implement Python's logging module to manage logs from multiple modules. Understand both simple and advanced methods to create, configure, and format loggers. This lesson helps you produce well-structured logs that identify the source module and log level for easier debugging and monitoring.

We'll cover the following...

The more you code, the more often you end up creating a set of custom modules that work together. If you want them all to log to the same place, then you’ve come to the right place. We’ll look at the simple way and then show a more complex method that’s also more customizable. Here’s one easy way to do it:

Python 3.5
import logging
import otherMod
def main():
"""
The main entry point of the application
"""
logging.basicConfig(filename="mySnake.log", level=logging.INFO)
logging.info("Program started")
result = otherMod.add(7, 8)
logging.info("Done!")
if __name__ == "__main__":
main()

Here we import logging and a module of our own creation (“otherMod”). Then we create our log file as before. The ...