Python Logging: Debugging with Style


Introduction
Logging is an essential aspect of Python development. It allows you to record important information, errors, and messages from your application, making it easier to troubleshoot issues and monitor its behaviour.
Basic Logging Setup
In Python, you can set up logging using the built-in logging
module. Here are the fundamental steps:
import logging
# Configure logging
logging.basicConfig(
level=logging.DEBUG, # Set the desired logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('app.log'), # Save log messages to a file
logging.StreamHandler() # Display log messages in the console
]
)
# Create a logger
logger = logging.getLogger(__name__)
# Example usage
logger.debug('This is a debug message')
logger.info('This is an info message')
logger.warning('This is a warning message')
logger.error('This is an error message')
logger.critical('This is a critical message')
Logging Levels
Python's logging
module provides different log levels to categorize your messages:
DEBUG
: Detailed information for debugging.INFO
: General information about the application's operations.WARNING
: Indicating something unexpected, but not an error.ERROR
: A genuine error that needs attention.CRITICAL
: A critical error that may lead to the application's termination.
You can set the logging level to control which messages get recorded.
Logging to Files
By using logging.FileHandler
, you can save log messages to a file. This is useful for long-running applications and for historical analysis.
Logging to Console
Using logging.StreamHandler
, you can also display log messages in the console, which is helpful for real-time debugging.
Logging Best Practices
Use different loggers for different parts of your application.
Consider rotating log files to prevent them from growing indefinitely.
Avoid using
print()
for debugging; use appropriate log levels instead.Customize log formats to include timestamps, source information, etc.
In production, configure logging to send logs to a central server for monitoring.
Further Reading
A Comprehensive Guide to Python Logging (this best usage guide on this topic on the internet; a personal opinion)
Loguru: An alternative logging library with a simpler syntax.
Conclusion
Effective logging is an essential skill for every Python developer. By following these steps and best practices, you can set up logging in your Python applications to improve their maintainability and reliability.
Subscribe to my newsletter
Read articles from Nikhil Akki directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nikhil Akki
Nikhil Akki
I am a Full Stack Solution Architect at Deloitte LLP. I help build production grade web applications on major public clouds - AWS, GCP and Azure.