Created
September 14, 2014 17:58
-
-
Save lebedov/11259fb7b25ebd47b256 to your computer and use it in GitHub Desktop.
Exception handler that outputs exception data to twiggy logger.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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