From 42cd0622e89fbf75ba0ec9e38fa84cbe6ad1cdf2 Mon Sep 17 00:00:00 2001 From: Albert Tugushev Date: Fri, 4 Nov 2022 17:21:38 +0100 Subject: [PATCH] Log repr of incoming message only in debug mode (#80) Resolved #74. --- ansq/tcp/connection.py | 7 ++++--- ansq/tcp/types/response_schemas.py | 13 ++++++++----- ansq/utils.py | 9 ++++++--- tests/test_utils.py | 26 ++++++++++++++------------ 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/ansq/tcp/connection.py b/ansq/tcp/connection.py index 3faa2b2..b4e9166 100644 --- a/ansq/tcp/connection.py +++ b/ansq/tcp/connection.py @@ -26,7 +26,7 @@ ) from ansq.tcp.types import TCPConnection as NSQConnectionBase from ansq.typedefs import TCPResponse -from ansq.utils import truncate_text, validate_topic_channel_name +from ansq.utils import validate_topic_channel_name # Auto reconnect settings AUTO_RECONNECT_INITIAL_INTERVAL = 2 @@ -402,7 +402,8 @@ async def _parse_data(self) -> bool: await self._pulse() return True - self.logger.debug("NSQ: Got data: %s", truncate_text(str(response))) + if self._debug: + self.logger.debug("NSQ: Got data: %s", response) if response.is_message: assert isinstance(response, NSQMessageSchema) @@ -420,7 +421,7 @@ async def _parse_data(self) -> bool: # non-error responses must have a command waiter, otherwise, # it's more likely a bug if not self._cmd_waiters: # pragma: no cover - self.logger.error("Unexpected response: %s", truncate_text(response.text)) + self.logger.error("Unexpected response: %s", response) return False future, callback = self._cmd_waiters.popleft() diff --git a/ansq/tcp/types/response_schemas.py b/ansq/tcp/types/response_schemas.py index ee67aa1..38aba4f 100644 --- a/ansq/tcp/types/response_schemas.py +++ b/ansq/tcp/types/response_schemas.py @@ -1,5 +1,6 @@ from typing import Union +from ...utils import truncate from . import FrameType, NSQCommands @@ -16,8 +17,9 @@ def __init__(self, body: bytes, frame_type: Union[FrameType, int]) -> None: ) def __repr__(self) -> str: - return "".format( - self.frame_type, self.body, self.is_ok + return ( + f"" ) def __bool__(self) -> bool: @@ -70,9 +72,10 @@ def __init__( def __repr__(self) -> str: return ( - "" - ).format(self.frame_type, self.body, self.timestamp, self.attempts, self.id) + f"" + ) class NSQErrorSchema(NSQResponseSchema): diff --git a/ansq/utils.py b/ansq/utils.py index d3b96a0..d65f784 100644 --- a/ansq/utils.py +++ b/ansq/utils.py @@ -179,9 +179,12 @@ def get_logger( return logger -def truncate_text(text: str, limit: int = 256) -> str: - """Truncate a given `text` if the `limit` is reached""" +def truncate(data: bytes, limit: int = 256) -> bytes: + """Truncate a given `data` if the `limit` is reached""" if limit <= 0: raise ValueError("limit must be greater than 0") - return text[:limit] + "..." if len(text) > limit else text + if len(data) <= limit: + return data + + return data[:limit] + b"..." diff --git a/tests/test_utils.py b/tests/test_utils.py index 2999acc..af5d244 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,29 +1,31 @@ import pytest -from ansq.utils import truncate_text +from ansq.utils import truncate @pytest.mark.parametrize( "text, limit, expected", ( - pytest.param("0123456789", 11, "0123456789", id="no trunc, greater than limit"), - pytest.param("0123456789", 10, "0123456789", id="no trunc, equal limit"), - pytest.param("0123456789", 9, "012345678...", id="trunc"), - pytest.param("0123456789", 1, "0...", id="minimal trunc"), - pytest.param("", 10, "", id="empty string"), + pytest.param( + b"0123456789", 11, b"0123456789", id="no trunc, greater than limit" + ), + pytest.param(b"0123456789", 10, b"0123456789", id="no trunc, equal limit"), + pytest.param(b"0123456789", 9, b"012345678...", id="trunc"), + pytest.param(b"0123456789", 1, b"0...", id="minimal trunc"), + pytest.param(b"", 10, b"", id="empty string"), ), ) -def test_truncate_text(text, limit, expected): - assert truncate_text(text, limit) == expected +def test_truncate(text, limit, expected): + assert truncate(text, limit) == expected @pytest.mark.parametrize( "text, limit", ( - pytest.param("0123456789", 0, id="zero"), - pytest.param("0123456789", -1, id="negative"), + pytest.param(b"0123456789", 0, id="zero"), + pytest.param(b"0123456789", -1, id="negative"), ), ) -def test_truncate_text_raises_value_error(text, limit): +def test_truncate_raises_value_error(text, limit): with pytest.raises(ValueError, match=r"^limit must be greater than 0$"): - assert truncate_text(text, limit) + assert truncate(text, limit)