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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Logging in Python: Conclusion

Give Feedback

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.

Resources

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.

Rob Black on Jan. 9, 2020

Excellent. Just the right level of detail. Good pace. Really helpful - thanks.

AllenJones on April 17, 2020

Great course!

Alan ODannel on June 12, 2020

Very good course. I’ll continue down the logging course path from here.

Ghani on Oct. 25, 2020

Very useful course; thanks!

Become a Member to join the conversation.