HomeLinuxPrint Stacktrace in Pyhton Log

Print Stacktrace in Pyhton Log


The “Stacktrace” is a report that shows the desired program state when an exception/error happens. For instance, when the Python program fails to execute the code, it would create a Stacktrace to inform the person what went improper. The Stacktrace consists of varied data, together with the error kind, the road quantity the place the error/exception occurred, and others. Due to this fact, printing the Stacktrace within the Python log helps us debug the code and discover the supply of the error.

This information will current you with a complete tutorial on printing Stacktrace utilizing quite a few examples.

Find out how to Print Stacktrace in Python Log?

The next strategies are used to log the error and print the Stacktrace in a Python log:

Methodology 1: Print Stacktrace in Python Log Utilizing “traceback” Module

The “traceback.print_exc()” technique of the “traceback” module is used to print the Stacktrace. To show this, take the next instance:

import traceback
num1 = [4, 3, 5, 2]
strive:
    worth = num1[5]
    print(worth)
besides:
    traceback.print_exc()

 

Within the above code:

  • The “traceback” module is imported.
  • The checklist named “num1” is initialized with 4 components.
  • The “strive” block executes the desired syntax “num1[5]” to get the ingredient worth of the index “5”.
  • The “traceback.print_exc()” technique of the “traceback” module is used to print the exception message and Stacktrace.

Output

Print Stacktrace in Pyhton Log

Methodology 2: Print Stacktrace in Python Log Utilizing “logging“ Module

The “logging” module supplies a method to create and handle log messages for this system. “Logging” is beneficial for debugging and monitoring errors or occasions that happen in the course of the execution of this system. The “logging” module makes use of varied strategies to log or print the error together with the exception.

For example, the “logging.error()” and “logging.exception()” strategies are each used to log errors in Python. Listed below are just a few examples to show this explicit technique:

Instance 1: Print Stacktrace in Python Utilizing “logging.exception()”

The “logging.exception()” technique logs the error message and the Stacktrace with out taking any parameter worth. This technique supplies extra details about the error, which may be useful for debugging. Here’s a code:

import logging
num1 = [4, 3, 5, 2]
strive:
    worth = num1[5]
    print(worth)
besides Exception:
    logging.exception(‘Exception Took Place.’)

 

On this code:

  • The “logging” library is imported, and the checklist is initialized.
  • The “try-except” block is utilized to catch/fetch any exception occurring in this system.
  • Within the besides block, the “logging.exception()” technique logs the error message together with the Stacktrace.

Output

The above output shows the error message together with the exception kind, the road quantity the place it occurred, and the traceback (the sequence of operate calls that led to the error).

Instance 2: Print Stacktrace in Python Utilizing “logging.error()”

The “logging.error()” merely logs the error in Python. To print the Stacktrace, this technique takes the “exc_info=True” as a parameter. Let’s overview the next code to print the Stacktrace within the Python log:

import logging
num1 = [4, 3, 5, 2]
strive:
    worth = num1[5]
    print(worth)
besides Exception as e:
    logging.error(‘Exception Took Place.’, exc_info=True

 

Within the above code:

  • Within the besides block, the “logging.error()” technique of the “logging” module logs an error message “Exception Took Place” by together with the details about the exception utilizing the parameter “exc_info=True”.

Output

The above output shows the error message together with the Stacktrace.

Conclusion

The “traceback” or “logging“ module supplies a number of capabilities which can be utilized in Python to log the error message and the Stacktrace. The “traceback.print_exc()” technique of the “traceback” module or the “logging.error()”, “logging.exception()” technique of the “logging” module is used to print exception data and Stacktrace. This tutorial has provided an in depth information on the way to print/show the Stacktrace in Python log.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments