Python tutorials > Error Handling > Exceptions > How to log exceptions?
How to log exceptions?
Logging exceptions in Python is crucial for debugging and monitoring applications. It allows you to track errors, understand their causes, and identify patterns that can help improve your code's reliability. This tutorial explores various methods to effectively log exceptions, providing practical examples and best practices.
Basic Exception Logging
This example demonstrates the most basic way to log an exception. First, we import the logging
module. Then, we configure the logger using logging.basicConfig()
. We set the logging level to logging.ERROR
, which means only error messages and above will be logged. We specify the filename where logs will be stored (error.log
), set the file mode to 'w' (write, overwriting previous content), and define the log message format to include timestamp, level, and message. Inside the try...except
block, we catch the ZeroDivisionError
and log it using logging.error()
. The message includes a description of the error and the exception object e
, which contains details about the error.
import logging
logging.basicConfig(level=logging.ERROR, filename='error.log', filemode='w', format='%(asctime)s - %(levelname)s - %(message)s')
try:
result = 10 / 0
except ZeroDivisionError as e:
logging.error(f'Division by zero: {e}')
Concepts Behind the Snippet
The logging
module provides a flexible and powerful way to handle logging in Python applications. It allows you to control the level of detail in your logs, direct logs to different destinations (files, console, etc.), and customize the format of your log messages. Key concepts include:
DEBUG
, INFO
, WARNING
, ERROR
, CRITICAL
. Each level indicates the severity of the event being logged.
Logging Exception Information with exc_info
The exc_info=True
argument in the logging.error()
(or any other logging level method) allows you to include the full exception traceback in the log message. This is extremely helpful for debugging because it provides a detailed stack trace that shows the sequence of function calls leading to the error. Without exc_info=True
, you only get the exception message, not the traceback. In this example, we define a function my_function
that might raise an exception. If an exception occurs, we log it with exc_info=True
, which will include the traceback in the 'error.log' file.
import logging
logging.basicConfig(level=logging.ERROR, filename='error.log', filemode='w', format='%(asctime)s - %(levelname)s - %(message)s')
def my_function(a, b):
try:
result = a / b
return result
except Exception as e:
logging.error("Exception occurred", exc_info=True)
return None
my_function(10, 0)
Real-Life Use Case Section
Consider a web application that handles user requests. When an unexpected error occurs during request processing (e.g., database connection error, invalid user input), it's crucial to log the error, along with relevant information about the request (user ID, URL, request parameters). This allows you to identify and fix issues quickly, improve the application's stability, and provide a better user experience. For example: In this example, we're logging errors that occur when processing data for a specific user. The log message includes the user ID and the exception information. This makes it easier to trace errors back to specific users and requests.import logging
from flask import Flask, request
app = Flask(__name__)
logging.basicConfig(level=logging.ERROR, filename='web_app_errors.log')
@app.route('/process_data')
def process_data():
try:
user_id = request.args.get('user_id')
# Simulate a potential error
if user_id == '123':
raise ValueError('Invalid user ID')
return 'Data processed successfully'
except Exception as e:
logging.error(f'Error processing data for user {user_id}: {e}', exc_info=True)
return 'An error occurred'
Best Practices
DEBUG
for detailed information during development, INFO
for general application events, WARNING
for potential issues, ERROR
for errors that don't crash the application, and CRITICAL
for errors that cause the application to terminate.RotatingFileHandler
.
Interview Tip
When discussing exception handling in interviews, highlight your understanding of different exception types, the importance of logging, and your ability to use Example response: "I always wrap critical sections of code in try...except
blocks effectively. Be prepared to explain how you would handle specific error scenarios and how you would use logging to diagnose issues.try...except
blocks to handle potential exceptions gracefully. I use the logging
module to record errors, including the full traceback using exc_info=True
. This helps me understand the root cause of issues and resolve them quickly. I also make sure to choose the appropriate log level for each type of event and include relevant context in my log messages."
When to use them
Log exceptions in the following scenarios:
Alternatives
While the built-in logging
module is powerful, other options exist:
Pros of Logging Exceptions
Cons of Logging Exceptions
FAQ
-
How do I prevent log files from growing too large?
Use theRotatingFileHandler
class from thelogging.handlers
module to automatically rotate log files when they reach a certain size. You can configure the maximum file size and the number of backup files to keep. -
How do I log exceptions to the console instead of a file?
Instead of specifying a filename inlogging.basicConfig()
, remove thefilename
argument. Log messages will then be printed to the console by default. -
How do I log custom data along with exceptions?
Include the custom data in the log message using f-strings or the%
operator. For example:logging.error(f'Error processing user {user_id}: {e}')