Skip to content

Avoid printing an ERROR log on failed connection#76

Open
pniedzielski wants to merge 1 commit into
aatikturk:mainfrom
pniedzielski:no-exception-traceback
Open

Avoid printing an ERROR log on failed connection#76
pniedzielski wants to merge 1 commit into
aatikturk:mainfrom
pniedzielski:no-exception-traceback

Conversation

@pniedzielski

Copy link
Copy Markdown

If a client attempts to connect to OBS, and OBS is not running, this library will print out a full traceback of an exception as an ERROR. This is bad behavior: in the case of a daemon-like program that automatically connects or reconnects to OBS, this will spew multiline ERROR traceback logs for the routine case that OBS has not started up yet, which can easily and correctly be handled by the program handling the re-raised exception itself. Furthermore, it is hard to correctly suppress this log outside of this library: the program needs to add a handler that filters out obsws_python log records only while attempting to connect, and only on the thread that is attempting to connect, lest we fail to log other ERRORs happening on different obsws connections:

class _SuppressObsLogsHandler(logging.Handler):
    """Temporarily discard log records from obsws_python on the creating thread."""

    def __init__(self):
        super().__init__(level=logging.DEBUG)
        self._thread_id = threading.get_ident()

    def filter(self, record):
        if (
            threading.get_ident() == self._thread_id
            and record.name.startswith("obsws_python")
        ):
            return False
        return True

    def emit(self, record):
        # Forward to lastResort for records we aren't suppressing, since
        # our presence on root already prevents lastResort from firing.
        if logging.lastResort and record.levelno >= logging.lastResort.level:
            logging.lastResort.handle(record)

This patch changes the logging level for failed connections from ERROR to DEBUG. This allows a program using this library to suppress DEBUG logging on the library much more easily than above while still being able to log an ERROR themselves if the failed connection truly is erroneous.

If a client attempts to connect to OBS, and OBS is not running, this
library will print out a full traceback of an exception as an ERROR.
This is bad behavior: in the case of a daemon-like program that
automatically connects or reconnects to OBS, this will spew multiline
ERROR traceback logs for the routine case that OBS has not started up
yet, which can easily and correctly be handled by the program handling
the re-raised exception itself.  Furthermore, it is hard to correctly
suppress this log outside of this library: the program needs to add a
handler that filters out `obsws_python` log records only while
attempting to connect, and only on the thread that is attempting to
connect, lest we fail to log other ERRORs happening on different
obsws connections:

```
class _SuppressObsLogsHandler(logging.Handler):
    """Temporarily discard log records from obsws_python on the creating thread."""

    def __init__(self):
        super().__init__(level=logging.DEBUG)
        self._thread_id = threading.get_ident()

    def filter(self, record):
        if (
            threading.get_ident() == self._thread_id
            and record.name.startswith("obsws_python")
        ):
            return False
        return True

    def emit(self, record):
        # Forward to lastResort for records we aren't suppressing, since
        # our presence on root already prevents lastResort from firing.
        if logging.lastResort and record.levelno >= logging.lastResort.level:
            logging.lastResort.handle(record)
```

This patch changes the logging level for failed connections from ERROR
to DEBUG.  This allows a program using this library to suppress DEBUG
logging on the library much more easily than above while still being
able to log an ERROR themselves if the failed connection truly is
erroneous.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant