From 89224302869ccbb4c34d6bd888a93f0cbcede7c8 Mon Sep 17 00:00:00 2001 From: Sebastian Correa <43179146+sebastian-correa@users.noreply.github.com> Date: Fri, 31 Jan 2025 19:00:39 -0300 Subject: [PATCH] Ensure before_sleep_log message isn't multiline Some exceptions add extra `\n` characters to their message, causing the last . in the log to be a new line. --- .../notes/multline-error-log-fix-f4791dd41c9915a3.yaml | 5 +++++ tenacity/before_sleep.py | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 releasenotes/notes/multline-error-log-fix-f4791dd41c9915a3.yaml diff --git a/releasenotes/notes/multline-error-log-fix-f4791dd41c9915a3.yaml b/releasenotes/notes/multline-error-log-fix-f4791dd41c9915a3.yaml new file mode 100644 index 00000000..b6834117 --- /dev/null +++ b/releasenotes/notes/multline-error-log-fix-f4791dd41c9915a3.yaml @@ -0,0 +1,5 @@ +fixes: + - | + Strip the ``str`` representation of the exception to avoid multiline + log output from ``before_sleep()`` when some exceptions include ``\n`` + characters at the end of their string representation. diff --git a/tenacity/before_sleep.py b/tenacity/before_sleep.py index 153edb7a..1dcf3ac1 100644 --- a/tenacity/before_sleep.py +++ b/tenacity/before_sleep.py @@ -46,7 +46,8 @@ def log_it(retry_state: "RetryCallState") -> None: if retry_state.outcome.failed: ex = retry_state.outcome.exception() - verb, value = "raised", f"{ex.__class__.__name__}: {ex}" + exception_str = str(ex).strip() # Ensure message isn't multiline. + verb, value = "raised", f"{ex.__class__.__name__}: {exception_str}" if exc_info: local_exc_info = retry_state.outcome.exception()