Skip to content

Commit 0a5a79c

Browse files
committed
Clean up prefixes of debug log messages.
All debug messages and only debug messages should have them.
1 parent 76f6f57 commit 0a5a79c

File tree

7 files changed

+30
-17
lines changed

7 files changed

+30
-17
lines changed

docs/topics/logging.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ Here's what websockets logs at each level.
218218
``ERROR``
219219
.........
220220

221-
* Exceptions raised by connection handler coroutines in servers
221+
* Exceptions raised by your code in servers
222+
* connection handler coroutines
223+
* ``select_subprotocol`` callbacks
224+
* ``process_request`` and ``process_response`` callbacks
222225
* Exceptions resulting from bugs in websockets
223226

224227
``WARNING``
@@ -250,4 +253,5 @@ Debug messages have cute prefixes that make logs easier to scan:
250253
* ``=`` - set connection state
251254
* ``x`` - shut down connection
252255
* ``%`` - manage pings and pongs
253-
* ``!`` - handle errors and timeouts
256+
* ``-`` - timeout
257+
* ``!`` - error, with a traceback

src/websockets/asyncio/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ async def __aiter__(self) -> AsyncIterator[ClientConnection]:
521521
delays = backoff()
522522
delay = next(delays)
523523
self.logger.info(
524-
"! connect failed; reconnecting in %.1f seconds",
524+
"connect failed; reconnecting in %.1f seconds",
525525
delay,
526526
exc_info=True,
527527
)

src/websockets/asyncio/connection.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -785,7 +785,7 @@ async def keepalive(self) -> None:
785785
self.logger.debug("% received keepalive pong")
786786
except asyncio.TimeoutError:
787787
if self.debug:
788-
self.logger.debug("! timed out waiting for keepalive pong")
788+
self.logger.debug("- timed out waiting for keepalive pong")
789789
async with self.send_context():
790790
self.protocol.fail(
791791
CloseCode.INTERNAL_ERROR,
@@ -866,7 +866,7 @@ async def send_context(
866866
await self.drain()
867867
except Exception as exc:
868868
if self.debug:
869-
self.logger.debug("error while sending data", exc_info=True)
869+
self.logger.debug("! error while sending data", exc_info=True)
870870
# While the only expected exception here is OSError,
871871
# other exceptions would be treated identically.
872872
wait_for_close = False
@@ -1042,7 +1042,7 @@ def data_received(self, data: bytes) -> None:
10421042
self.send_data()
10431043
except Exception as exc:
10441044
if self.debug:
1045-
self.logger.debug("error while sending data", exc_info=True)
1045+
self.logger.debug("! error while sending data", exc_info=True)
10461046
self.set_recv_exc(exc)
10471047

10481048
if self.protocol.close_expected():

src/websockets/legacy/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,14 +603,14 @@ async def __aiter__(self) -> AsyncIterator[WebSocketClientProtocol]:
603603
if backoff_delay == self.BACKOFF_MIN:
604604
initial_delay = random.random() * self.BACKOFF_INITIAL
605605
self.logger.info(
606-
"! connect failed; reconnecting in %.1f seconds",
606+
"connect failed; reconnecting in %.1f seconds",
607607
initial_delay,
608608
exc_info=True,
609609
)
610610
await asyncio.sleep(initial_delay)
611611
else:
612612
self.logger.info(
613-
"! connect failed again; retrying in %d seconds",
613+
"connect failed again; retrying in %d seconds",
614614
int(backoff_delay),
615615
exc_info=True,
616616
)

src/websockets/legacy/protocol.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,7 +1246,7 @@ async def keepalive_ping(self) -> None:
12461246
self.logger.debug("% received keepalive pong")
12471247
except asyncio.TimeoutError:
12481248
if self.debug:
1249-
self.logger.debug("! timed out waiting for keepalive pong")
1249+
self.logger.debug("- timed out waiting for keepalive pong")
12501250
self.fail_connection(
12511251
CloseCode.INTERNAL_ERROR,
12521252
"keepalive ping timeout",
@@ -1288,7 +1288,7 @@ async def close_connection(self) -> None:
12881288
if await self.wait_for_connection_lost():
12891289
return
12901290
if self.debug:
1291-
self.logger.debug("! timed out waiting for TCP close")
1291+
self.logger.debug("- timed out waiting for TCP close")
12921292

12931293
# Half-close the TCP connection if possible (when there's no TLS).
12941294
if self.transport.can_write_eof():
@@ -1306,7 +1306,7 @@ async def close_connection(self) -> None:
13061306
if await self.wait_for_connection_lost():
13071307
return
13081308
if self.debug:
1309-
self.logger.debug("! timed out waiting for TCP close")
1309+
self.logger.debug("- timed out waiting for TCP close")
13101310

13111311
finally:
13121312
# The try/finally ensures that the transport never remains open,
@@ -1332,7 +1332,7 @@ async def close_transport(self) -> None:
13321332
if await self.wait_for_connection_lost():
13331333
return
13341334
if self.debug:
1335-
self.logger.debug("! timed out waiting for TCP close")
1335+
self.logger.debug("- timed out waiting for TCP close")
13361336

13371337
# Abort the TCP connection. Buffers are discarded.
13381338
if self.debug:

src/websockets/sync/connection.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,10 @@ def recv_events(self) -> None:
640640
data = self.socket.recv(self.recv_bufsize)
641641
except Exception as exc:
642642
if self.debug:
643-
self.logger.debug("error while receiving data", exc_info=True)
643+
self.logger.debug(
644+
"! error while receiving data",
645+
exc_info=True,
646+
)
644647
# When the closing handshake is initiated by our side,
645648
# recv() may block until send_context() closes the socket.
646649
# In that case, send_context() already set recv_exc.
@@ -665,7 +668,10 @@ def recv_events(self) -> None:
665668
self.send_data()
666669
except Exception as exc:
667670
if self.debug:
668-
self.logger.debug("error while sending data", exc_info=True)
671+
self.logger.debug(
672+
"! error while sending data",
673+
exc_info=True,
674+
)
669675
# Similarly to the above, avoid overriding an exception
670676
# set by send_context(), in case of a race condition
671677
# i.e. send_context() closes the socket after recv()
@@ -783,7 +789,10 @@ def send_context(
783789
self.send_data()
784790
except Exception as exc:
785791
if self.debug:
786-
self.logger.debug("error while sending data", exc_info=True)
792+
self.logger.debug(
793+
"! error while sending data",
794+
exc_info=True,
795+
)
787796
# While the only expected exception here is OSError,
788797
# other exceptions would be treated identically.
789798
wait_for_close = False

tests/legacy/test_client_server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1615,12 +1615,12 @@ async def run_client():
16151615
[
16161616
"connection rejected (503 Service Unavailable)",
16171617
"connection closed",
1618-
"! connect failed; reconnecting in X seconds",
1618+
"connect failed; reconnecting in X seconds",
16191619
]
16201620
+ [
16211621
"connection rejected (503 Service Unavailable)",
16221622
"connection closed",
1623-
"! connect failed again; retrying in X seconds",
1623+
"connect failed again; retrying in X seconds",
16241624
]
16251625
* ((len(logs.records) - 8) // 3)
16261626
+ [

0 commit comments

Comments
 (0)