Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Resources

Avatar image for Malef

Malef on July 24, 2019

Hello.

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

Thanks, RealPython.

Avatar image for avermaisi11

avermaisi11 on July 25, 2019

How to append the date to the log file name ?

Avatar image for Rob Black

Rob Black on July 29, 2019

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

Avatar image for Kalesis

Kalesis on July 30, 2019

Excelent!

Avatar image for Mike

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)

Avatar image for pa1

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?

Avatar image for mattnhb

mattnhb on Sept. 18, 2019

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

Avatar image for Rob Black

Rob Black on Jan. 9, 2020

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

Avatar image for AllenJones

AllenJones on April 17, 2020

Great course!

Avatar image for Alan ODannel

Alan ODannel on June 12, 2020

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

Avatar image for Ghani

Ghani on Oct. 25, 2020

Very useful course; thanks!

Avatar image for Emmanuel Jolaiya

Emmanuel Jolaiya on May 23, 2021

Awesome course!

Avatar image for Brannen Taylor

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

Avatar image for vaklaf

vaklaf on March 28, 2022

Very useful course, thanks!

Avatar image for Ash D

Ash D on Jan. 7, 2024

To answer the earlier poster’s question about inserting datestamping into the filename, or for anyone else wondering, using the TimedRotatingFileHandler might help. It inserts a date-time stamp into the filename, although it is primarily designed to rotate logfiles at a preset interval or time of day or day of week. See here, and also RotatingFileHandler for a filesize-based rotating file handler. docs.python.org/3/library/logging.handlers.html#logging.handlers.TimedRotatingFileHandler

Avatar image for Ash D

Ash D on Jan. 7, 2024

Good intro course to logging. It would be useful to have a follow-up course (or expand this course), which covers the following:

  1. How to use the same logger across different classes and modules (spoiler: the logging module can do this seamlessly)
  2. Guidelines on what you should log, and why, and at which levels.
  3. Guilelines on when you should print vs. when you should log, with different scenarios, eg. a script vs. an interactive console app vs. a Flask/Django app, etc. The console app would obviously use print for normal interactions, and logging to file and/or console for errors etc. A script that processes hundreds of files would almost exclusively use file logging, presumably.
  4. Tips on the Rotating File Handler classes within the logging module, which helps with auto-rotating logfiles above a certain size, etc.
  5. A link or mention to whatever RealPython course would teach the user how to save all that boilerplate logging setup code for easy reuse (it’s off-topic for the logging course, but this was a question I thought of, particularly if you really prefer to do the config inside the code rather than config file).

Become a Member to join the conversation.