Logging in Python
The logging
module in Python allows developers to track events that happen during the execution of a program. It’s an essential tool for debugging, monitoring, and understanding how the code behaves at runtime. Unlike using print statements, logging provides a flexible framework for categorizing, formatting, and routing log messages.
Why Use Logging?
Using the logging module has several advantages over simple print statements:
- Granular Control: You can categorize logs by severity (debug, info, warning, error, critical).
- File Logging: Write logs to files for better tracking of application behavior.
- Log Formatting: Include timestamps and other context to make log messages more informative.
- External Handlers: Send logs to external systems using handlers like HTTP, email, etc.
Setting Up a Basic Logger
To set up basic logging, use the basicConfig()
method:
import logging
# Create a basic logger that outputs to the console
logging.basicConfig(level=logging.INFO)
logging.info("This is an info message")
Explanation:
The basicConfig()
method configures the default logging behavior. In this example, we set the logging level to INFO
, meaning all log messages of level INFO
and above will be displayed.
Logging Levels
There are five standard logging levels in Python:
- DEBUG: Detailed information, typically of interest only when diagnosing problems.
- INFO: Confirmation that things are working as expected.
- WARNING: An indication that something unexpected happened, or indicative of some problem in the near future.
- ERROR: More serious problems.
- CRITICAL: A very serious error, indicating that the program may be unable to continue running.
Customizing Log Format
You can customize how the log messages appear using the format
parameter in basicConfig()
:
import logging
# Customize the log format
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO)
logging.info("This message has a custom format")
Explanation:
In this example, we’ve customized the log output format to include the timestamp (asctime)
, log level (levelname)
, and the log message (message)
.
Logging to a File
Instead of printing logs to the console, you can write them to a file:
import logging
# Log messages to a file
logging.basicConfig(filename='app.log', filemode='w', level=logging.INFO)
logging.info("This message will be written to the log file")
Explanation:
The filename
parameter specifies the log file’s name, and filemode='w'
ensures that the log file is overwritten each time the program runs. Use filemode='a'
to append to the log file instead.
Using Handlers
Handlers allow you to send logs to different destinations, like files or external services:
import logging
# Create a custom logger
logger = logging.getLogger('my_logger')
# Create a file handler
file_handler = logging.FileHandler('my_log.log')
# Set formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(formatter)
# Add handler to logger
logger.addHandler(file_handler)
logger.setLevel(logging.INFO)
logger.info("Logging to the file using handlers")
Explanation:
In this example, we use a FileHandler
to log messages to a file with a custom format. The logger is configured with a handler that formats and routes log messages.
Key Points to Remember
- Use different log levels for debugging and monitoring applications.
- Customize log formats for better clarity and traceability.
- Use handlers to route logs to different destinations, like files, streams, or external systems.
Import Links
Here are some useful import links for further reading: