Logging in Python
Python Shorts — Part 7
Logging is a powerful introspection tool, and a must for any professional software project. With it, we can keep track of our application’s execution, follow progress, find bugs and overall improve debugging.
Luckily, Python offers a convenient standard library logging module, satisfying all these requirements.

One good question to start with is: why use logging over simple prints? There are several reasons:
- Many other Python modules use this standard library logging module, thus we also pick up their log prints along the way.
- Logging offers much convenience functionality for the designated use-cases, such as automatically printing the line from where it was called, and supports different log levels (more below).
- Logging offers many customisation options, such as formatting, sending the output to files and other targets, etc.
With that said, let’s jump in the code and give it a first try:
import logging
logging.debug('DEBUG')
logging.info('INFO')
logging.warning('WARNING')
logging.error('ERROR')
logging.critical('CRITICAL')Upon running this, we should only see three outputs: WARNING / ERROR / CRITICAL.
This is because the default log level is set to warning, i.e. everything below warning is not printed.
To change that, we add a single line after the import:
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug('DEBUG')
logging.info('INFO')
logging.warning('WARNING')
logging.error('ERROR')
logging.critical('CRITICAL')… and now see all the output.
As teased in the beginning, there is a multitude of ways to customise logging behaviour. However, I’ll be brief and end this post here, as in my professional experience already these steps help in developing a scalable software architecture. Hope you enjoyed!
This post is part of a series show-casing important Python concepts quickly. You can find the other parts here:
- Part 1: Lambda Functions in Python
- Part 2: Iterators in Python
- Part 3: Generators and Generator Expressions in Python
- Part 4: Advanced Iteration in Python with enumerate() and zip()
- Part 5: Managing Resources in Python with Context Managers (with statement)
- Part 6: Generating Temporary Files and Directories in Python
- Part 8: Partial Functions in Python
- Part 9: f-Strings in Python
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.






