Skip to content

Instantly share code, notes, and snippets.

@mgedmin
Last active December 13, 2015 17:58
Show Gist options
  • Save mgedmin/4952018 to your computer and use it in GitHub Desktop.
Save mgedmin/4952018 to your computer and use it in GitHub Desktop.
Today's best Python code (from ZConfig/components/logger/tests/test_logger.py)
class CustomFormatter(logging.Formatter):
def formatException(self, ei):
"""Format and return the exception information as a string.
This adds helpful advice to the end of the traceback.
"""
import traceback
sio = StringIO.StringIO()
traceback.print_exception(ei[0], ei[1], ei[2], file=sio)
return sio.getvalue() + "... Don't panic!"
@florentx
Copy link

or simply

    ...
    return "".join(traceback.format_exception(*ei)) + "... Don't panic!"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment