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
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
00:00
In this course, we learned how to use the logging
module’s built-in logger for quick logging. We learned how to customize our own loggers for more advanced logging.
00:10 And we learned how to use a configuration file to configure our logger.
00:16 Logging is a great way to learn more about your application. When it comes to large-scale applications, it becomes a lot more useful than simple print statements, and it’s a great way to get information about how users are using your program.
00:30 It can also provide helpful information that’s crucial to determining the best way to scale your program. Many people think of logging as an afterthought, and it’s not until things go wrong that they wish they had implemented it.
00:45 Hopefully, this course has shown you the power of logging in Python. If you want to learn more, I’d recommend you check out our other article titled Python Logging: A Stroll Through the Source Code, which goes into greater detail about all the stuff we covered here.
01:01 I’m Austin Cepalia with realpython.com, and I wish you the best of luck in your programming endeavors. Happy coding!
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!
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
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:
- How to use the same logger across different classes and modules (spoiler: the logging module can do this seamlessly)
- Guidelines on what you should log, and why, and at which levels.
- 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.
- Tips on the Rotating File Handler classes within the logging module, which helps with auto-rotating logfiles above a certain size, etc.
- 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.
Malef on July 24, 2019
Hello.
logging.info("Malef's gratitude for clear explanation of useful topic is recorded.")
Thanks, RealPython.