...

/

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

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

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:

Press + to interact
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 ...

Access this course and 1400+ top-rated courses and projects.