Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log errors less frequently #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 11 additions & 7 deletions pysonofflan/sonoffdevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(self,

async def setup_connection(self, retry):
self.logger.debug('setup_connection is active on the event loop')
level = logging.WARN # level to log with
retry_count = 0

while True:
Expand All @@ -93,26 +93,30 @@ async def setup_connection(self, retry):
self.logger.debug(
'setup_connection yielding to send_online_message()')
await self.client.send_online_message()

connected = True

except websockets.InvalidMessage as ex:
self.logger.warn('Unable to connect: %s' % ex)
await self.wait_before_retry(retry_count)
except ConnectionRefusedError:
self.logger.warn('Unable to connect: connection refused')
self.logger.log( level, 'Unable to connect: connection refused')
await self.wait_before_retry(retry_count)
except websockets.exceptions.ConnectionClosed:
self.logger.warn('Connection closed unexpectedly during setup')
self.logger.log( level, 'Connection closed unexpectedly during setup')
await self.wait_before_retry(retry_count)
except OSError as ex:
self.logger.warn('OSError in setup_connection(): %s', format(ex) )
self.logger.log( level, 'OSError in setup_connection(): %s', format(ex) )
await self.wait_before_retry(retry_count)
except Exception as ex:
self.logger.error('Unexpected error in setup_connection(): %s', format(ex) )
self.logger.log( level, 'Unexpected error in setup_connection(): %s', format(ex) )
await self.wait_before_retry(retry_count)

finally:
level = logging.DEBUG # if we've had a error and logged it, don't log again

if connected:
level = logging.WARN # if we've had a error and logged it, don't log again

retry_count = 0 # reset retry count after successful connection
try:
self.logger.debug(
Expand All @@ -122,7 +126,7 @@ async def setup_connection(self, retry):
except websockets.InvalidMessage as ex:
self.logger.warn('Unable to connect: %s' % ex)
except websockets.exceptions.ConnectionClosed:
self.logger.warn('Connection closed in receive_message_loop()')
self.logger.debug('Connection closed in receive_message_loop()')
except OSError as ex:
self.logger.warn('OSError in receive_message_loop(): %s', format(ex) )

Expand Down