
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Best Way to Log a Python Exception
The best way to log Python exceptions is by using the built-in logging module. It helps you track errors and debug your programs by capturing detailed error information.
This module allows you to control where the logs are saved and organize them by their importance and source. Using logging.exception() function inside except blocks is an easy way to log errors along with the full traceback.
Why Use the logging Module for Exceptions?
The logging module allows you to save error messages with details like when they happened and how serious they are. It gives you more control and useful information than just printing errors on the screen.
Example
In the following example, we catch an exception and log it using the logging.error() function by setting the exc_info parameter to True to include the traceback -
import logging logging.basicConfig(level=logging.ERROR) try: result = 10 / 0 except ZeroDivisionError: logging.error("An error occurred", exc_info=True)
The output will show the error message along with the full traceback -
ERROR:root:An error occurred Traceback (most recent call last): File "/home/cg/root/681af9f2c7256/main.py", line 6, in <module> result = 10 / 0 ~~~^~~ ZeroDivisionError: division by zero
Using logging.exception() Function
The logging.exception() function is a shortcut for logging an error message with exception information. It should be called inside an except block and automatically adds the traceback.
Example
In this example, we catch a ValueError and log it with logging.exception() function -
import logging logging.basicConfig(level=logging.ERROR) try: x = int("abc") except ValueError: logging.exception("Conversion failed")
This produces output like the following with traceback -
ERROR:root:Conversion failed Traceback (most recent call last): File "/home/cg/root/681af9f2c7256/main.py", line 6, in <module> x = int("abc") ^^^^^^^^^^ ValueError: invalid literal for int() with base 10: 'abc'
Configuring logging output to files
You can configure the logging module to save exception logs to files instead of printing them to the console. This is useful for long-term storage and auditing.
Example
In the following example, we are saving logs including exceptions to the error.log file -
import logging logging.basicConfig(filename='error.log', level=logging.ERROR) try: open("nonexistent.txt") except FileNotFoundError: logging.exception("File not found error") print ('File Saved')
The full error with traceback will be saved in error.log file -
File Saved
Using Custom loggers
For larger applications, create custom loggers with specific names and handlers for better control over what gets logged and where.
Example
In this example, we create a custom logger and log an exception -
import logging logger = logging.getLogger('my_logger') logging.basicConfig(level=logging.ERROR) try: data = [1, 2, 3] print(data[5]) except IndexError: logger.exception("Index error occurred")
Output will include the exception details with the logger name -
ERROR:my_logger:Index error occurred Traceback (most recent call last): File "/home/cg/root/681af9f2c7256/main.py", line 8, in <module> print(data[5]) ~~~~^^^ IndexError: list index out of range