Created
March 3, 2020 16:11
-
-
Save nip3o/eb938f898c379cd847cba3af49b69cb7 to your computer and use it in GitHub Desktop.
This file contains 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
# Tweak the existing Logging integration to work with Logbook. | |
# Mostly copy-paste from https://github.com/getsentry/sentry-python/blob/master/sentry_sdk/integrations/logging.py | |
class SentryHandler(logbook.Handler): | |
def emit(self, record): | |
with capture_internal_exceptions(): | |
self.format(record) | |
return self._emit(record) | |
def _emit(self, record): | |
hub = Hub.current | |
if hub.client is None: | |
return | |
client_options = hub.client.options | |
# exc_info might be None or (None, None, None) | |
if record.exc_info is not None and record.exc_info[0] is not None: | |
event, hint = event_from_exception( | |
record.exc_info, | |
client_options=client_options, | |
mechanism={"type": "logbook", "handled": True}, | |
) | |
elif record.exc_info and record.exc_info[0] is None: | |
event = {} | |
hint = {} | |
with capture_internal_exceptions(): | |
event["threads"] = { | |
"values": [ | |
{ | |
"stacktrace": current_stacktrace( | |
client_options["with_locals"] | |
), | |
"crashed": False, | |
"current": True, | |
} | |
] | |
} | |
else: | |
event = {} | |
hint = {} | |
hint["log_record"] = record | |
event["level"] = logbook.get_level_name(record.level).lower() | |
event["logger"] = record.channel | |
event["logentry"] = {"message": to_string(record.msg), "params": record.args} | |
event["extra"] = { | |
"lineno": record.lineno, | |
"filename": record.filename, | |
"function": record.func_name, | |
"process": record.process, | |
"process_name": record.process_name, | |
} | |
hub.capture_event(event, hint=hint) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment