Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Logging in Python: Conclusion

Logging is a very useful tool in a programmer’s toolbox. It can help you develop a better understanding of the flow of a program and discover scenarios that you might not even have thought of while developing.

Logs provide developers with an extra set of eyes that are constantly looking at the flow that an application is going through. They can store information, like which user or IP accessed the application. If an error occurs, then they can provide more insights than a stack trace by telling you what the state of the program was before it arrived at the line of code where the error occurred.

By logging useful data from the right places, you can not only debug errors easily but also use the data to analyze the performance of the application to plan for scaling or look at usage patterns to plan for marketing.

Python provides a logging system as a part of its standard library, so you can quickly add logging to your application. In this course, you learned why using this module is the best way to add logging to your application as well as how to get started quickly, and you will get an introduction to some of the advanced features available.

Comments & Discussion

Malef on July 24, 2019

Hello.

logging.info("Malef's gratitude for clear explanation of useful topic is recorded.")

Thanks, RealPython.

avermaisi11 on July 25, 2019

How to append the date to the log file name ?

Rob Black on July 29, 2019

Very useful course - I’ve already added simple logging to my app based on the lessons here. Thanks!

Kalesis on July 30, 2019

Excelent!

Mike on Aug. 14, 2019

If you want the date/time in the log file name, you could do something like…

from datetime import datetime

time = datetime.now() file_out = time.strftime(“%Y-%m-%d %H:%M:%S”) + “.log”

file_handler = logging.FileHandler(file_out)

pa1 on Sept. 8, 2019

all these examples were using main module.

However, If I want to write a package with multiple python modules (multiple .py files) and then create a python script which import one or more modules from this package, what is the correct way to implement logging?

Does each module (.py file) will have a logger (with its own StreamHandler and FileHandlers) at the beginning of the file? If yes, how many log files gets created and which module’s logger formatter will take the preference etc?

mattpystar on Sept. 18, 2019

Thanks for the tutorial! You made it look very simple and easy for me.

Become a Member to join the conversation.