To answer your question, you can get a line version of print_exception() using the traceback.format_exception() function. It returns a trace message as a list of strings, and does not print it to stdout, so you can do what you want. For example:
import sys import traceback try: asdf except NameError: exc_type, exc_value, exc_traceback = sys.exc_info() lines = traceback.format_exception(exc_type, exc_value, exc_traceback) print ''.join('!! ' + line for line in lines)
Displayed:
!! Traceback (most recent call last): !! File "<stdin>", line 2, in <module> !! NameError: name 'asdf' is not defined
However, I would definitely recommend using the standard Python logging module, as suggested by rlotun. This is not the easiest setting, but it is very customizable.
Ben Hoyt Dec 22 2018-10-22 14:32
source share