Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix exception logging in Python2 #15

Merged
merged 1 commit into from
Jul 25, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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