How can I log an exception in Python?
I've looked at some options and found out I can access the actual exception details using this code:
import sys
import traceback
try:
1/0
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
I would like to somehow get the string print_exception() throws to stdout so that I can log it.
raise(without argument, so the stracktrace gets preserved) after logging, otherwise you swallow the exception silently.except NameError as e, say. That will prevent you catching things likeKeyboardInterruptand give you a reference to the exception object, which you can study for more details.