Python catch any exception, and print or log traceback with variable values
Question:
When I catch unexpected error with sys.excepthook
import sys
import traceback
def handleException(excType, excValue, trace):
print 'error'
traceback.print_exception(excType, excValue, trace)
sys.excepthook = handleException
h = 1
k = 0
print h/k
This is output I get
error
Traceback (most recent call last):
File "test.py", line 13, in <module>
print h/k
ZeroDivisionError: integer division or modulo by zero
How can I include variable values (h, k, …) in traceback simillar to http://www.doughellmann.com/PyMOTW/cgitb/ ? When I include cgitb result is same.
EDIT:
Great answer I only modified it like this so it logs trace in a file
def handleException(excType, excValue, trace):
cgitb.Hook(logdir=os.path.dirname(__file__),
display=False,
format='text')(excType, excValue, trace)
Answers:
By looking at the source of cgitb.py
, you should be able to use something like this:
import sys
import traceback
import cgitb
def handleException(excType, excValue, trace):
print 'error'
cgitb.Hook(format="text")(excType, excValue, trace)
sys.excepthook = handleException
h = 1
k = 0
print h/k
As the best practice you should avoid adding dubug info in your output or exceptions, there are tools to help you with that with a single line of code. Here’s an example:
Have a look at some related packages. For simple usage you might pick traceback-with-variables (pip install traceback-with-variables
), here is it’s postcard
Or try tbvaccine, or better-exceptions, or any other package
IPython has a built-in option to include variable values in Tracebacks.
Just run your script as follows:
ipython --InteractiveShell.xmode=Verbose script.py
You will get a full traceback, code context, and the values of relevant variables.
When I catch unexpected error with sys.excepthook
import sys
import traceback
def handleException(excType, excValue, trace):
print 'error'
traceback.print_exception(excType, excValue, trace)
sys.excepthook = handleException
h = 1
k = 0
print h/k
This is output I get
error
Traceback (most recent call last):
File "test.py", line 13, in <module>
print h/k
ZeroDivisionError: integer division or modulo by zero
How can I include variable values (h, k, …) in traceback simillar to http://www.doughellmann.com/PyMOTW/cgitb/ ? When I include cgitb result is same.
EDIT:
Great answer I only modified it like this so it logs trace in a file
def handleException(excType, excValue, trace):
cgitb.Hook(logdir=os.path.dirname(__file__),
display=False,
format='text')(excType, excValue, trace)
By looking at the source of cgitb.py
, you should be able to use something like this:
import sys
import traceback
import cgitb
def handleException(excType, excValue, trace):
print 'error'
cgitb.Hook(format="text")(excType, excValue, trace)
sys.excepthook = handleException
h = 1
k = 0
print h/k
As the best practice you should avoid adding dubug info in your output or exceptions, there are tools to help you with that with a single line of code. Here’s an example:
Have a look at some related packages. For simple usage you might pick traceback-with-variables (pip install traceback-with-variables
), here is it’s postcard
Or try tbvaccine, or better-exceptions, or any other package
IPython has a built-in option to include variable values in Tracebacks.
Just run your script as follows:
ipython --InteractiveShell.xmode=Verbose script.py
You will get a full traceback, code context, and the values of relevant variables.