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()