Printing errors with print() can only take you so far. For scalable, professional Python applications, using the built-in logging module is the right way to debug and track issues.
This beginner-friendly guide explains what the logging module is, why it matters, and how you can start using it effectively in your Python projects.
Why Use the logging Module?
- Centralized way to track events in your app
- More flexible than print() for real-time debugging
- Supports logging levels (info, debug, warning, error, critical)
- Can write logs to files, not just the console
- Helps in production, automation, and error tracking
Basic Logging Example
python
Copy code
import logging
logging.basicConfig(level=logging.DEBUG)
logging.debug(“This is a debug message”)
logging.info(“Informational message”)
logging.warning(“This is a warning”)
logging.error(“An error occurred”)
logging.critical(“Critical issue”)
By default, basicConfig() sends logs to the console.
Logging to a File
python
Copy code
logging.basicConfig(filename=”app.log”, level=logging.INFO, format=”%(asctime)s – %(levelname)s – %(message)s”)
logging.info(“Application started”)
✅ Logs will be saved in app.log with timestamps and severity levels.
Understanding Logging Levels
| Level | Use Case |
| DEBUG | Detailed info, mostly for developers |
| INFO | General info about program execution |
| WARNING | Something unexpected, but recoverable |
| ERROR | More serious issue; might crash soon |
| CRITICAL | Major failure; program can’t continue |
Use levels to control the verbosity of logs and filter unnecessary messages.
Custom Logger Example
python
Copy code
logger = logging.getLogger(“my_app”)
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler(“my_app.log”)
formatter = logging.Formatter(“%(name)s – %(levelname)s – %(message)s”)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info(“Custom logger initialized”)
This setup gives you full control over where and how logs are recorded.
Practice Tip
Convert one of your Python scripts to use logging instead of print(). Set it up to log both to the console and to a file. This is especially useful for debugging long-running scripts or automation tools.
Build Smarter, Debug Faster
Learning logging not only improves your debugging process—it also prepares your projects for real-world deployment.
🚀 Want hands-on practice with real-world Python tools and workflows?
👉 https://www.thefullstack.co.in/courses/
You might be like this:-
What is AWS Lambda?A Beginner’s Guide to Serverless Computing in 2025
Java vs. Kotlin: Which One Should You Learn for Backend Development?
Where to Find Your Salesforce Organization ID
How Salesforce Stands Out from Other CRMs
Frequently Asked Questions
What is Python’s logging module and how does it help with debugging?
Python’s logging module is a built-in module that allows you to record events happening during the execution of your program. It helps with debugging by providing a way to track and log important events, errors, and exceptions, making it easier to identify and fix issues. By using the logging module, you can gain more insight into your program’s behavior and performance.
How do I configure the logging module to suit my needs?
To configure the logging module, you can set the logging level, format, and output destination. You can use the basicConfig function to set up the logging module with a few simple parameters, or you can create a more complex configuration using handlers and formatters. By configuring the logging module, you can control what information is logged and how it is presented.
What are the different logging levels available in the logging module?
The logging module provides five built-in logging levels: DEBUG, INFO, WARNING, ERROR, and CRITICAL. Each level represents a different severity of event, with DEBUG being the most verbose and CRITICAL being the most severe. By using these logging levels, you can categorize and prioritize the events in your program and control what information is logged.
Can I use the logging module to log events to a file instead of the console?
Yes, you can use the logging module to log events to a file instead of the console. To do this, you can create a FileHandler and add it to the logger, specifying the file path and name. This allows you to persist log data even after the program has finished running, making it easier to analyze and debug issues.
How can I use the logging module in a multi-threaded or multi-process environment?
In a multi-threaded or multi-process environment, you can use the logging module’s thread-safe and process-safe features to ensure that log messages are handled correctly. You can use the QueueHandler and QueueListener classes to handle log messages from multiple threads or processes, and to ensure that log messages are not interleaved or lost. This allows you to safely use the logging module in concurrent environments.

