Python's built-in logging module is a powerful tool for tracking events that occur while your script is running. It's not just about debugging; logging can prov...

Python's built-in logging module is a powerful tool for tracking events that occur while your script is running. It's not just about debugging; logging can provide valuable insights into your application's behavior, helping you optimize performance and maintain a healthy codebase. Let's dive into Python logging with practical examples.

Python logging offers several benefits:


To start using Python logging, you'll first need to import the logging module:
import logging
Then, you can configure the logger with various attributes like the log level, format, and handlers. Here's a basic setup:
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

Python logging has five levels, in increasing order of severity:
You can set the log level to filter messages based on their importance.





















Here's a simple example demonstrating the use of different log levels:
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')
You can redirect logs to a file using the `FileHandler`. This is useful for persistent logging and reviewing logs later:
file_handler = logging.FileHandler('app.log')
logging.basicConfig(handlers=[file_handler], level=logging.INFO)
You can customize the log format to include additional information like the logger name or function name:
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s - %(funcName)s')
Instead of using the root logger (`logging`), you can create named loggers to organize your logs:
logger = logging.getLogger(__name__)
logger.debug('This is a debug message from %s', __name__)
For long-running applications, you might want to rotate log files to prevent them from growing too large. The `RotatingFileHandler` can help with this:
from logging.handlers import RotatingFileHandler
file_handler = RotatingFileHandler('app.log', maxBytes=1024*1024*5, backupCount=5)
logging.basicConfig(handlers=[file_handler], level=logging.INFO)
The log file will be rotated when it reaches 5MB, and up to 5 rotated files will be kept.
Here are some best practices for using Python logging:
Python logging is a versatile tool that can help you build more robust and maintainable applications. By understanding and leveraging its features, you can gain valuable insights into your code's behavior and make data-driven decisions.