When writing programs in Python, errors and unexpected behavior are common. To understand what is happening inside your code, developers use a technique called logging. Logging helps you track events, errors, and important information while your program is running.
Unlike simple print statements, logging is more powerful, flexible, and suitable for real-world applications. In this blog, we will understand what logging is, why it is important, and how to use it in Python with simple examples.

What is Logging in Python?
Logging is a way to record messages about the execution of a program. These messages can include:
- Errors
- Warnings
- Information messages
- Debug details
Python provides a built-in module called logging that makes it easy to implement logging in your applications.
Why Use Logging Instead of Print?
Many beginners use print() statements to debug their code. However, logging is much better because:
- It provides different severity levels (info, warning, error, etc.)
- Messages can be saved in files
- It is easier to control and format output
- Suitable for large applications
- Helps in debugging production systems
In short, logging is a professional way to track program behavior.
Basic Logging in Python
Python’s logging module is simple to use. Let’s start with a basic example.
import logginglogging.basicConfig(level=logging.INFO)logging.info("This is an info message")
logging.warning("This is a warning message")
logging.error("This is an error message")
Explanation:
basicConfig()sets up basic configurationlevel=logging.INFOsets the minimum log level- Different log functions display different types of messages
Logging Levels in Python
Python provides different logging levels:
1. DEBUG
Used for detailed information, mainly for debugging.
2. INFO
Used for general information about program execution.
3. WARNING
Indicates something unexpected, but not an error.
4. ERROR
Indicates a serious problem in the program.
5. CRITICAL
Indicates a very serious error that may stop the program.
Example of Logging Levels
import logginglogging.basicConfig(level=logging.DEBUG)logging.debug("Debugging information")
logging.info("Program is running")
logging.warning("This is a warning")
logging.error("An error occurred")
logging.critical("Critical issue detected")
Each level helps you understand the severity of events in your program.
Logging to a File
Instead of showing logs on the screen, you can save them in a file.
import logginglogging.basicConfig(
filename='app.log',
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s'
)logging.info("Application started")
logging.warning("Low memory warning")
logging.error("An error occurred")
Benefits:
- Keeps a record of program activity
- Useful for debugging later
- Helps in production environments
Customizing Log Format
You can customize how logs are displayed using format strings.
Example:
import logginglogging.basicConfig(
format='%(levelname)s:%(message)s',
level=logging.DEBUG
)logging.info("Custom formatted log message")
You can also include:
- Time (
%(asctime)s) - Log level (
%(levelname)s) - Function name (
%(funcName)s)
Logging in Functions
Logging is very useful inside functions to track execution flow.
import logginglogging.basicConfig(level=logging.INFO)def add(a, b):
logging.info("Adding two numbers")
return a + bresult = add(5, 3)
logging.info(f"Result is {result}")
This helps you understand what is happening inside your program.
Best Practices for Logging
To use logging effectively, follow these best practices:
- Use appropriate logging levels
- Avoid using print statements for debugging
- Always log errors and exceptions
- Use file logging in production
- Keep log messages clear and meaningful
Logging vs Debugging
| Logging | Debugging |
|---|---|
| Records program events | Finds and fixes errors |
| Runs during execution | Used during development |
| Stored in files or console | Interactive process |
Logging helps you understand what went wrong, while debugging helps you fix it.
Real-World Uses of Logging
Logging is widely used in:
- Web applications
- Banking systems
- E-commerce platforms
- APIs and backend systems
- Machine learning projects
It helps developers monitor system performance and detect issues quickly.
Logging in Python is an essential skill for every developer. It helps you track program behavior, identify errors, and improve code quality.
By using Python’s logging module, you can build more reliable and professional applications. Whether you are a beginner or an advanced programmer, mastering logging will make debugging easier and your projects more efficient.
Start using logging in your Python programs today and take your coding skills to the next level.
For More Information and Updates, Connect With Us
- Name Sumit singh
- Phone Number: +91 9264477176
- Email ID: emancipationedutech@gmail.com
- Our Platforms:
- Digilearn Cloud
- Live Emancipation
- Follow Us on Social Media:
- Instagram – Emancipation
- Facebook – Emancipation
Stay connected and keep learning with Python Training !