Skip to content

Commit 300d73a

Browse files
committed
update truncation
1 parent 5a13932 commit 300d73a

3 files changed

Lines changed: 10 additions & 7 deletions

File tree

runpod/serverless/modules/rp_logger.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ def log(self, message, message_level='INFO', job_id=None):
7676
# Truncate message over 10MB, remove chunk from the middle
7777
if len(message) > MAX_MESSAGE_LENGTH:
7878
half_max_length = MAX_MESSAGE_LENGTH // 2
79-
message = message[:half_max_length] + '\n...TRUNCATED...\n' + message[-half_max_length:]
79+
truncated_amount = len(message) - MAX_MESSAGE_LENGTH
80+
truncation_note = f'\n...TRUNCATED {truncated_amount} CHARACTERS...\n'
81+
message = message[:half_max_length] + truncation_note + message[-half_max_length:]
8082

8183
if os.environ.get('RUNPOD_ENDPOINT_ID'):
8284
log_json = {

tests/test_serverless/test_modules/test_logger.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,19 @@ def test_log_job_id(self):
135135
)
136136

137137
def test_log_truncate(self):
138-
"""Tests that the log method truncates long messages."""
138+
"""Tests that the log method truncates """
139139
logger = rp_logger.RunPodLogger()
140140
job_id = "test_job_id"
141-
long_message = "a" * (rp_logger.MAX_MESSAGE_LENGTH + 100) # Exceeds the max length
141+
long_message = "a" * (rp_logger.MAX_MESSAGE_LENGTH + 100)
142142
expected_start = "a" * (rp_logger.MAX_MESSAGE_LENGTH // 2)
143143
expected_end = "a" * (rp_logger.MAX_MESSAGE_LENGTH // 2)
144-
truncated_message = expected_start + '\n...TRUNCATED...\n' + expected_end
144+
truncated_amount = len(long_message) - rp_logger.MAX_MESSAGE_LENGTH
145+
truncation_note = f'\n...TRUNCATED {truncated_amount} CHARACTERS...\n'
146+
truncated_message = expected_start + truncation_note + expected_end
145147

146148
with patch("builtins.print") as mock_print:
147149
logger.log(long_message, "INFO", job_id)
148150

149-
# Construct the expected log output
150151
expected_log_output = f'INFO | {job_id} | {truncated_message}'
151152

152153
mock_print.assert_called_once_with(expected_log_output, flush=True)

tests/test_serverless/test_worker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ async def test_run_worker_generator_handler_exception(
274274
except KeyError:
275275
pass
276276

277-
with patch("os.environ.get") as mock_get:
278-
mock_get.return_value = None
277+
with patch("runpod.serverless.worker.os") as mock_os:
278+
mock_os.environ.get.return_value = None
279279
runpod.serverless.start(generator_config)
280280

281281
assert mock_stream_result.call_count == 1

0 commit comments

Comments
 (0)