The “Stacktrace” is a report that displays the specified program state when an exception/error occurs. For example, when the Python program fails to execute the code, it will create a Stacktrace to tell the user what went wrong. The Stacktrace includes various information, including the error type, the line number where the error/exception occurred, and others. Therefore, printing the Stacktrace in the Python log helps us debug the code and find the source of the error.
This guide will present you with a comprehensive tutorial on printing Stacktrace using numerous examples.
How to Print Stacktrace in Python Log?
The following methods are used to log the error and print the Stacktrace in a Python log:
Method 1: Print Stacktrace in Python Log Using “traceback” Module
The “traceback.print_exc()” method of the “traceback” module is used to print the Stacktrace. To demonstrate this, take the following example:
num1 = [4, 3, 5, 2]
try:
value = num1[5]
print(value)
except:
traceback.print_exc()
In the above code:
- The “traceback” module is imported.
- The list named “num1” is initialized with four elements.
- The “try” block executes the specified syntax “num1[5]” to get the element value of the index “5”.
- The “traceback.print_exc()” method of the “traceback” module is used to print the exception message and Stacktrace.
Output
Method 2: Print Stacktrace in Python Log Using “logging“ Module
The “logging” module provides a way to create and manage log messages for the program. “Logging” is useful for debugging and tracking errors or events that occur during the execution of the program. The “logging” module uses various methods to log or print the error along with the exception.
For instance, the “logging.error()” and “logging.exception()” methods are both used to log errors in Python. Here are a few examples to demonstrate this particular method:
Example 1: Print Stacktrace in Python Using “logging.exception()”
The “logging.exception()” method logs the error message and the Stacktrace without taking any parameter value. This method provides more information about the error, which can be helpful for debugging. Here is a code:
num1 = [4, 3, 5, 2]
try:
value = num1[5]
print(value)
except Exception:
logging.exception('Exception Took Place.')
In this code:
- The “logging” library is imported, and the list is initialized.
- The “try-except” block is utilized to catch/fetch any exception occurring in the program.
- In the except block, the “logging.exception()” method logs the error message along with the Stacktrace.
Output
The above output displays the error message along with the exception type, the line number where it occurred, and the traceback (the sequence of function calls that led to the error).
Example 2: Print Stacktrace in Python Using “logging.error()”
The “logging.error()” simply logs the error in Python. To print the Stacktrace, this method takes the “exc_info=True” as a parameter. Let’s overview the following code to print the Stacktrace in the Python log:
num1 = [4, 3, 5, 2]
try:
value = num1[5]
print(value)
except Exception as e:
logging.error('Exception Took Place.', exc_info=True)
In the above code:
- In the except block, the “logging.error()” method of the “logging” module logs an error message “Exception Took Place” by including the information about the exception using the parameter “exc_info=True”.
Output
The above output displays the error message along with the Stacktrace.
Conclusion
The “traceback” or “logging“ module provides several functions that are used in Python to log the error message and the Stacktrace. The “traceback.print_exc()” method of the “traceback” module or the “logging.error()”, “logging.exception()” method of the “logging” module is used to print exception information and Stacktrace. This tutorial has offered a detailed guide on how to print/display the Stacktrace in Python log.