Skip to content

Commit

Permalink
Use conditional log level
Browse files Browse the repository at this point in the history
Signed-off-by: Ben Johnson <ben@binarylogic.com>
  • Loading branch information
binarylogic committed Apr 11, 2024
1 parent b74c89e commit 9e7a87e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions trinnov_altitude/trinnov_altitude.py
Original file line number Diff line number Diff line change
Expand Up @@ -544,13 +544,19 @@ async def _listen(self, reconnect: bool, reconnect_backoff: float):
This method will automatically reconnect when necessary if `reconnect`
is set to `True`.
"""
failure_count = 0

while True:
try:
raw_message = await self._read(timeout=None)
except (EOFError, OSError, Exception) as e:
failure_count += 1

if reconnect:
self.logger.error(
f"Unable to read message from Trinnov Altitude, reconnecting...: {e}"
level = logging.DEBUG if failure_count <= 1 else logging.ERROR
self.logger.log(
level,
f"Unable to read message from Trinnov Altitude, reconnecting...: {e}",
)

try:
Expand All @@ -564,6 +570,7 @@ async def _listen(self, reconnect: bool, reconnect_backoff: float):
raise e
else:
self._process_message(raw_message)
failure_count = 0

async def _read(self, timeout: float | None) -> str:
"""Read a single raw message off of the socket"""
Expand Down

0 comments on commit 9e7a87e

Please sign in to comment.