In this post, I cover everything from logging to configuring logging to output messages to different destinations.
I also included some examples of logging levels and how to log messages at different levels based on the severity of the issue.
I hope my post will help anyone understand how to use logging effectively in their Python programs. If you have any thoughts or suggestions, feel free to share them with me.
What Is the Python logging Module
The Python logging module is a standard-library component that records timestamped, severity-tagged messages from a running program and routes them to destinations such as the console, files, or remote services. Logging is essential for developers to track events, debug issues, and understand how their programs work, and it offers a flexible alternative to print() for production diagnostics.
Logging allows us to:
- Track the flow of your program
- Debug and diagnose issues
- Monitor applications in production
- Gain insights into user behaviour
Python logging Examples
Python’s logging module is simple and can be configured to suit different needs. Let’s start with a basic example.
Basic Python logging Configuration Example
We import Python’s built-in logging module with the import logging statement.
Next, the logging.basicConfig(level=logging.INFO) line configures the logging system to capture messages at the INFO level and higher. The logging.info('This is an informational message.') line logs an informational message, which will be displayed because the logging level is set to INFO.
import logging
# Configure the basic logger
logging.basicConfig(level=logging.INFO)
# Log a simple message
logging.info('This is an informational message.')
The output:
INFO:root:This is an informational message.
In this configuration, the format of the log messages includes the time, the name of the logger, the log level, and the actual log message. The datefmt parameter specifies the format of the date.
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S')
Logging can be configured to output messages to different destinations (console, files, etc.) and in various formats. The logging module also supports different severity levels.
Python Logging Levels: DEBUG, INFO, WARNING, ERROR, CRITICAL
Python’s logging module has several built-in levels:
- DEBUG: Detailed information, typically of interest only when diagnosing problems.
- INFO: Confirmation that things are working as expected.
- WARNING: An indication that something unexpected happened or indicative of some problem soon.
- ERROR: Due to a more severe problem, the software cannot perform some functions.
- CRITICAL: A grave error indicating that the program itself cannot continue running.
Each level is represented by an integer value:
DEBUG: 10
INFO: 20
WARNING: 30
ERROR: 40
CRITICAL: 50
The critical-level examples, file logging and the full handler setup follow below.
Advanced Logging Configuration
🔒 Subscribe to keep reading.
Conclusion: When to Use the Python logging Module
🔒 Subscribe to keep reading.
References
🔒 Subscribe to keep reading.
You've hit a Deep Dive tutorial.
I spend dozens of hours researching, coding, and breaking things to write these guides. This content is free, but reserved for my subscriber community. Drop your email below to unlock this guide (and all past/future deep dives):
Full content temporarily unavailable — refresh in a moment
Already a subscriber? Use the magic link from your last newsletter, or reset your password.
Log in to unlock
New subscribers get an inbox mail: Set a password to unlock articles. The form does not log you in — use the same email afterwards.