Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@moshekaplan
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moshekaplan/60519e92cbf5f9826fba to your computer and use it in GitHub Desktop.
Save moshekaplan/60519e92cbf5f9826fba to your computer and use it in GitHub Desktop.
import traceback
import logging
def get_exception_info(skip=2):
"""Note, this relies on being called from the exception handler. This is very brittle to the depth of the call."""
frames_output = []
for frame in traceback.extract_stack()[:-skip]:
fname, lineno, parent, function = frame
frame_output = """ File "%s", line %d, in %s\n %s""" % (fname, lineno, parent, function)
frames_output.append(frame_output)
msg = ''
msg += 'Parent stack' + '\n'
msg += '\n'.join(frames_output) + '\n'
msg += 'try stack' + '\n'
msg += '\n'.join(traceback.format_exc().split('\n')[1:])
return msg
def foo():
try:
1/0
except:
print "Using function - this is the I have yet, although it's extremely brittle'"
print get_exception_info()
print
print 'Logging sample'
logging.exception('EEK!')
print
print "'Normal' Traceback"
raise
def bar():
foo()
bar()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment