Skip to content

Commit

Permalink
Merge pull request #15 from kusha/fix-python2-exception
Browse files Browse the repository at this point in the history
Fix exception logging in Python2
  • Loading branch information
kusha authored Jul 25, 2019
2 parents c548c22 + 7925007 commit 2d4c0d1
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions kafka_logger/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,19 @@ def unhandled_exception(self, exctype, exception, traceback):
traceback (traceback): traceback object
"""
if self.unhandled_exception_logger is not None:
if sys.version_info[0] < 3: # Python 2 only
# built-in logging exception() call ignores exc_info
# it tries to get sys.exc_info for the second time
# sys.exc_info returns (None, None, None) w/o context
# this override fixes exception logging in Python 2
original_exc_info = sys.exc_info
sys.exc_info = lambda: (exctype, exception, traceback, )
self.unhandled_exception_logger.exception(
"Unhandled top-level exception",
exc_info=(exctype, exception, traceback, ))
if sys.version_info[0] < 3:
# remove exc_info override back
sys.exc_info = original_exc_info

def close(self):
"""Close the handler."""
Expand Down

0 comments on commit 2d4c0d1

Please sign in to comment.