Skip to content

Commit

Permalink
drop websocket logging level (#26)
Browse files Browse the repository at this point in the history
* drop websocket logging level

* docs
  • Loading branch information
Matt Kafonek authored Aug 11, 2022
1 parent 6ac0e11 commit 043ebec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,29 @@ nox -s lint
```
nox -s lint_check
```

## Websocket Logging

`WebsocketBackend` uses `structlog` for its logs. To view or suppress logs such as `Sending` and `Received` for all messages coming over the websocket, use this minimal structlog code.

```
import structlog
import logging
processors = [
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
]
structlog.configure(
processors=processors, logger_factory=structlog.stdlib.LoggerFactory()
)
logging.basicConfig()
# enable debug logs
logging.getLogger("sending.backends.websocket").setLevel(logging.DEBUG)
# suppress debug logs
logging.getLogger("sending.backends.websocket").setLevel(logging.INFO)
```
6 changes: 3 additions & 3 deletions sending/backends/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ async def _publish(self, message: QueuedMessage):
# special logging here because this is a sign that you might be in
# a particularly bad position. Something called .send() before
# callback to .on_auth or similar 'set the authed_ws Future' triggered.
logger.info("Message queued, waiting for authed_ws to be set")
logger.debug("Message queued, waiting for authed_ws to be set")
ws = await asyncio.wait_for(self.authed_ws, timeout=self.publish_timeout)
else:
ws = await asyncio.wait_for(self.unauth_ws, timeout=self.publish_timeout)
logger.info(f"Sending: {message.contents}")
logger.debug(f"Sending: {message.contents}")
await ws.send(message.contents)

async def _poll_loop(self):
Expand All @@ -154,7 +154,7 @@ async def _poll_loop(self):
fn = ensure_async(self.init_hook)
await fn(self)
async for message in websocket:
logger.info(f"Received: {message}")
logger.debug(f"Received: {message}")
self.schedule_for_delivery(topic="", contents=message)
except websockets.ConnectionClosed:
# This will get raised if there's an error trying to connect,
Expand Down

0 comments on commit 043ebec

Please sign in to comment.