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

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands 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 your subtitle preferences 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

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?

mattnhb 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!

Emmanuel Jolaiya on May 23, 2021

Awesome course!

Brannen Taylor on March 6, 2022

Thanks Austin - helpful - I will try to follow along and get a better feel for how to use custom loggers - this is the most confusing part for me - as I’ve implemented basic config.

I found a helpful format for me is to include module and function names int the logs, and then when I import my logging config, I can see it moving around in my logs.

format = '%(asctime)s : %(levelname)s : %(module)s : %(funcName)s : %(lineno)d : %(message)s',

More info here: 3.10 Logging Doc

vaklaf on March 28, 2022

Very useful course, thanks!

Become a Member to join the conversation.