python-websockets / websockets

Library for building WebSocket servers and clients in Python
https://websockets.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
5.06k stars 505 forks source link

Human-readable representation of a frame is too short #1451

Closed rayrapetyan closed 2 months ago

rayrapetyan commented 3 months ago

By default, length of the human-readable representation of a frame is limited by 75 characters: https://github.com/python-websockets/websockets/blob/e217458ef8b692e45ca6f66c5aeb7fad0aee97ee/src/websockets/frames.py#L189 This looks too restrictive and should be at least say 256.

aaugustin commented 3 months ago

This is always going to be too short for someone... and too long for someone else... 🙃

The use case for repr(frame) is: tell at a glance if the content looks like JSON. I don't think a generic log can do much more.

That being said, I'm open to making this constant easier to monkey patch.

aaugustin commented 3 months ago

e.g.

diff --git a/src/websockets/frames.py b/src/websockets/frames.py
index 201bc50..e1505cc 100644
--- a/src/websockets/frames.py
+++ b/src/websockets/frames.py
@@ -146,6 +146,8 @@ class Frame:
     rsv2: bool = False
     rsv3: bool = False

+    MAX_LOG = 75  # should be a multiple of 3
+
     def __str__(self) -> str:
         """
         Return a human-readable representation of a frame.
@@ -186,8 +188,8 @@ class Frame:
         else:
             data = "''"

-        if len(data) > 75:
-            data = data[:48] + "..." + data[-24:]
+        if len(data) > self.MAX_LOG:
+            data = data[:self.MAX_LOG * 2 // 3] + "..." + data[-self.MAX_LOG // 3:]

         metadata = ", ".join(filter(None, [coding, length, non_final]))