Skip to content
This repository has been archived by the owner on Nov 5, 2023. It is now read-only.

Commit

Permalink
Fix NoStacktraceFormatter
Browse files Browse the repository at this point in the history
This deals with a surprising gotcha in Python's logging system: the
result of formatException() is cached. If you want to ensure that a
custom formatException method is called, you need to explicitly clear
the exc_text attribute in format().

This is documented behavior, see https://bugs.python.org/issue29056
for background info.

In our setup, we had an earlier handler with a default formatter, so
NoStacktraceFormatter's method was entirely ignored.
  • Loading branch information
pdewacht committed Mar 12, 2019
1 parent e1bea1b commit 32df3d8
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions slacker_log_handler/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class NoStacktraceFormatter(Formatter):
def formatException(self, ei):
return None

def format(self, record):
# Work-around for https://bugs.python.org/issue29056
saved_exc_text = record.exc_text
record.exc_text = None
try:
return super(NoStacktraceFormatter, self).format(record)
finally:
record.exc_text = saved_exc_text


class SlackerLogHandler(Handler):
def __init__(self, api_key, channel, stack_trace=True, username='Python logger', icon_url=None, icon_emoji=None,
Expand Down

0 comments on commit 32df3d8

Please sign in to comment.