# Buggy code
def _log(event):
    if "log_text" in event:
        if event["log_text"].startswith("DNSDatagramProtocol starting on "):
            return

        if event["log_text"].startswith("(UDP Port "):
            return

        if event["log_text"].startswith("Timing out client"):
            return

    return observer(event)


# Traceback (UnicodeDecodeError)
2018-10-25 15:58:33,973 - twisted - 243 - ERROR - POST-299240- Traceback (most recent call last):
2018-10-25 15:58:33,973 - twisted - 243 - ERROR - POST-299240-   File "/usr/lib/python2.7/logging/handlers.py", line 76, in emit
2018-10-25 15:58:33,974 - twisted - 243 - ERROR - POST-299240-     if self.shouldRollover(record):
2018-10-25 15:58:33,974 - twisted - 243 - ERROR - POST-299240-   File "/usr/lib/python2.7/logging/handlers.py", line 156, in shouldRollover
2018-10-25 15:58:33,974 - twisted - 243 - ERROR - POST-299240-     msg = "%s\n" % self.format(record)
2018-10-25 15:58:33,974 - twisted - 243 - ERROR - POST-299240-   File "/usr/lib/python2.7/logging/__init__.py", line 734, in format
2018-10-25 15:58:33,975 - twisted - 243 - ERROR - POST-299240-     return fmt.format(record)
2018-10-25 15:58:33,975 - twisted - 243 - ERROR - POST-299240-   File "/usr/lib/python2.7/logging/__init__.py", line 476, in format
2018-10-25 15:58:33,975 - twisted - 243 - ERROR - POST-299240-     raise e
2018-10-25 15:58:33,977 - twisted - 243 - ERROR - POST-299240- UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 46: ordinal not in range(128)
2018-10-25 15:58:33,978 - twisted - 243 - ERROR - POST-299240- Logged from file _base.py, line 254
2018-10-25 15:58:33,978 - twisted - 243 - ERROR - POST-299240- Traceback (most recent call last):
2018-10-25 15:58:33,978 - twisted - 243 - ERROR - POST-299240-   File "/usr/lib/python2.7/logging/__init__.py", line 861, in emit
2018-10-25 15:58:33,978 - twisted - 243 - ERROR - POST-299240-     msg = self.format(record)
2018-10-25 15:58:33,979 - twisted - 243 - ERROR - POST-299240-   File "/usr/lib/python2.7/logging/__init__.py", line 734, in format
2018-10-25 15:58:33,979 - twisted - 243 - ERROR - POST-299240-     return fmt.format(record)
2018-10-25 15:58:33,979 - twisted - 243 - ERROR - POST-299240-   File "/usr/lib/python2.7/logging/__init__.py", line 476, in format
2018-10-25 15:58:33,979 - twisted - 243 - ERROR - POST-299240-     raise e
2018-10-25 15:58:33,980 - twisted - 243 - ERROR - POST-299240- UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 46: ordinal not in range(128)
2018-10-25 15:58:33,980 - twisted - 243 - ERROR - POST-299240- Logged from file _base.py, line 254
(etc)

# Fixed code
def _log(event):
    if "log_text" in event:
        if event["log_text"].startswith("DNSDatagramProtocol starting on "):
            return

        if event["log_text"].startswith("(UDP Port "):
            return

        if event["log_text"].startswith("Timing out client"):
            return

    # this is a workaround to make sure we don't get stack overflows when the
    # logging system raises an error which is written to stderr which is redirected
    # to the logging system, etc.
    if getattr(threadlocal, "active", False):
        # write the text of the event, if any, to the *real* stderr (which may
        # be redirected to /dev/null, but there's not much we can do)
        try:
            event_text = eventAsText(event)
            print("logging during logging: %s" % event_text, file=sys.__stderr__)
        except Exception:
            # gah.
            pass
        return

    try:
        threadlocal.active = True
        return observer(event)
    finally:
        threadlocal.active = False