From 9e7a87e3123ace006471c7e874155b3222b118de Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Thu, 11 Apr 2024 11:07:09 -0700 Subject: [PATCH] Use conditional log level Signed-off-by: Ben Johnson --- trinnov_altitude/trinnov_altitude.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/trinnov_altitude/trinnov_altitude.py b/trinnov_altitude/trinnov_altitude.py index d184517..b71e4db 100644 --- a/trinnov_altitude/trinnov_altitude.py +++ b/trinnov_altitude/trinnov_altitude.py @@ -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: @@ -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"""