Logging in Python
Logging in Python is a powerful way to track events, debug issues, and monitor
application behavior. Unlike print(), logging provides levels,
timestamps, and the ability to store logs in files.
Why Logging is Important
- Helps debug applications efficiently
- Provides detailed runtime information
- Useful in production environments
- Helps in monitoring and auditing
Logging Levels in Python
Python provides different logging levels based on severity:
- DEBUG – Detailed information, typically for debugging
- INFO – General information about application flow
- WARNING – Something unexpected happened
- ERROR – A serious problem occurred
- CRITICAL – Application may stop running
Basic Logging Example
A simple example of using Python’s built-in logging module:
import logging
logging.basicConfig(level=logging.INFO)
logging.debug("This is a DEBUG message")
logging.info("This is an INFO message")
logging.warning("This is a WARNING message")
logging.error("This is an ERROR message")
logging.critical("This is a CRITICAL message")
Logging with Custom Format
You can customize log messages to include timestamps, log levels, and messages.
import logging
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info("Application started")
logging.error("An error occurred")
Logging to a File
Logs can be saved to a file instead of printing on the console.
import logging
logging.basicConfig(
filename="app.log",
filemode="a",
level=logging.INFO,
format="%(asctime)s - %(levelname)s - %(message)s"
)
logging.info("Log written to file")
Using Logger Object
For larger applications, it is recommended to use a logger object.
import logging
logger = logging.getLogger("my_logger")
logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
formatter = logging.Formatter(
"%(asctime)s - %(name)s - %(levelname)s - %(message)s"
)
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
logger.debug("Debug message")
logger.info("Info message")
logger.error("Error message")
Logging Exceptions
Python logging can automatically capture stack traces for exceptions.
import logging
logging.basicConfig(level=logging.ERROR)
try:
result = 10 / 0
except Exception as e:
logging.exception("An exception occurred")
Best Practices for Logging
- Use logging instead of print statements
- Choose appropriate logging levels
- Avoid logging sensitive information
- Use log files for production systems
Logging in Automation Frameworks
Logging plays a key role in automation frameworks like:
- Robot Framework
- PyTest
- Selenium-based frameworks
Logs help analyze test failures and execution flow.
Conclusion
Logging in Python is essential for building reliable and maintainable applications. With proper logging practices, you can debug faster, monitor applications, and improve overall code quality.
Mastering Python logging is a must-have skill for developers, testers, and automation engineers.
No comments:
Post a Comment