Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions tenacity/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ class LoggerProtocol(typing.Protocol):
Compatible with logging, structlog, loguru, etc...
"""

def log(
self, level: int, msg: str, *args: typing.Any, **kwargs: typing.Any
) -> typing.Any: ...
def log(self, level: int, msg: str, *args: typing.Any) -> typing.Any: ...


def find_ordinal(pos_num: int) -> str:
Expand Down
23 changes: 11 additions & 12 deletions tenacity/before_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import traceback
import typing

from tenacity import _utils
Expand All @@ -35,8 +36,6 @@ def before_sleep_log(
"""Before sleep strategy that logs to some logger the attempt."""

def log_it(retry_state: "RetryCallState") -> None:
local_exc_info: BaseException | bool | None

if retry_state.outcome is None:
raise RuntimeError("log_it() called before outcome was set")

Expand All @@ -46,22 +45,22 @@ 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}"

if exc_info:
local_exc_info = retry_state.outcome.exception()
else:
local_exc_info = False
else:
verb, value = "returned", retry_state.outcome.result()
local_exc_info = False # exc_info does not apply when no exception

fn_name = retry_state.get_fn_name()

logger.log(
log_level,
msg = (
f"Retrying {fn_name} "
f"in {sec_format % retry_state.next_action.sleep} seconds as it {verb} {value}.",
exc_info=local_exc_info,
f"in {sec_format % retry_state.next_action.sleep} seconds as it {verb} {value}."
)

if exc_info and retry_state.outcome.failed:
ex = retry_state.outcome.exception()
if ex is not None:
tb = "".join(traceback.format_exception(type(ex), ex, ex.__traceback__))
msg = f"{msg}\n{tb.rstrip()}"

logger.log(log_level, msg)

return log_it