Skip to content

Instantly share code, notes, and snippets.

@lebedov
Created September 14, 2014 17:58
Show Gist options
  • Save lebedov/11259fb7b25ebd47b256 to your computer and use it in GitHub Desktop.
Save lebedov/11259fb7b25ebd47b256 to your computer and use it in GitHub Desktop.
Exception handler that outputs exception data to twiggy logger.
#!/usr/bin/env python
"""
Exception handler that outputs exception data to twiggy logger.
"""
import re
import sys
import traceback
import twiggy
output = twiggy.outputs.StreamOutput(twiggy.formats.line_format,
stream=sys.stdout)
twiggy.emitters['*'] = twiggy.filters.Emitter(twiggy.levels.DEBUG,
True, output)
def handler(type, value, tb):
msg = '|'.join([': '.join([y.strip() for y in x.strip('\n ').split('\n')]) for x in \
traceback.format_exception(type, value, tb)[1:]])
twiggy.log.error('Uncaught exception: %s' % str(msg))
sys.excepthook = handler
# Force an exception:
def func():
1/0
func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment