Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions homeassistant/util/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ def emit(self, record: logging.LogRecord) -> None:
except Exception: # pylint: disable=broad-except
self.handleError(record)

def handle(self, record: logging.LogRecord) -> Any:
"""
Conditionally emit the specified logging record.

Depending on which filters have been added to the handler, push the new
records onto the backing Queue.

The default python logger Handler acquires a lock
in the parent class which we do not need as
SimpleQueue is already thread safe.

See https://bugs.python.org/issue24645
"""
return_value = self.filter(record)
if return_value:
self.emit(record)
return return_value


@callback
def async_activate_log_queue_handler(hass: HomeAssistant) -> None:
Expand Down
11 changes: 11 additions & 0 deletions tests/util/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ async def test_logging_with_queue_handler():
):
handler.emit(log_record)

with patch.object(handler, "emit") as emit_mock:
handler.handle(log_record)
emit_mock.assert_called_once()

with patch.object(handler, "filter") as filter_mock, patch.object(
handler, "emit"
) as emit_mock:
filter_mock.return_value = False
handler.handle(log_record)
emit_mock.assert_not_called()

with patch.object(handler, "enqueue", side_effect=OSError), patch.object(
handler, "handleError"
) as mock_handle_error:
Expand Down