Python Logging: Debugging with Style
Setting up logging in Python is crucial for debugging, monitoring, and maintaining your code. Here's a concise guide on how to set up logging,

full stack dev | SaaS | AI | maker - builder
Search for a command to run...
Setting up logging in Python is crucial for debugging, monitoring, and maintaining your code. Here's a concise guide on how to set up logging,

full stack dev | SaaS | AI | maker - builder
No comments yet. Be the first to comment.
In this series, we will cover Web frameworks, tools and libraries from Python eco-system.
Integrating Celery with FastAPI for Asynchronous Tasks
Because sometimes your models need to gossip with each other

Whether you are a SysAdmin or Causal Linux (Ubuntu/Debian) User, here is a Guide to Rescue Your Development Environment Without Breaking a Sweat

Because Not All Code Guards Need to Break the Bank

Harnessing the Power of Pydantic for Seamless AI Integration

From Simple Chat-bots to Autonomous Digital Assistants

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.
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')
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.
By using logging.FileHandler, you can save log messages to a file. This is useful for long-running applications and for historical analysis.
Using logging.StreamHandler, you can also display log messages in the console, which is helpful for real-time debugging.
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.
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.