Skip to content
Merged
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
5 changes: 5 additions & 0 deletions releasenotes/notes/logging-protocol-a4cf0f786f21e4ee.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
other:
- |
Accept non-standard logger in helpers logging something (eg: structlog, loguru...)

12 changes: 12 additions & 0 deletions tenacity/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
MAX_WAIT = sys.maxsize / 2


class LoggerProtocol(typing.Protocol):
"""
Protocol used by utils expecting a logger (eg: before_log).

Compatible with logging, structlog, loguru, etc...
"""

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


def find_ordinal(pos_num: int) -> str:
# See: https://en.wikipedia.org/wiki/English_numerals#Ordinal_numbers
if pos_num == 0:
Expand Down
4 changes: 1 addition & 3 deletions tenacity/after.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from tenacity import _utils

if typing.TYPE_CHECKING:
import logging

from tenacity import RetryCallState


Expand All @@ -29,7 +27,7 @@ def after_nothing(retry_state: "RetryCallState") -> None:


def after_log(
logger: "logging.Logger",
logger: _utils.LoggerProtocol,
log_level: int,
sec_format: str = "%.3g",
) -> typing.Callable[["RetryCallState"], None]:
Expand Down
4 changes: 1 addition & 3 deletions tenacity/before.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from tenacity import _utils

if typing.TYPE_CHECKING:
import logging

from tenacity import RetryCallState


Expand All @@ -29,7 +27,7 @@ def before_nothing(retry_state: "RetryCallState") -> None:


def before_log(
logger: "logging.Logger", log_level: int
logger: _utils.LoggerProtocol, log_level: int
) -> typing.Callable[["RetryCallState"], None]:
"""Before call strategy that logs to some logger the attempt."""

Expand Down
4 changes: 1 addition & 3 deletions tenacity/before_sleep.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from tenacity import _utils

if typing.TYPE_CHECKING:
import logging

from tenacity import RetryCallState


Expand All @@ -29,7 +27,7 @@ def before_sleep_nothing(retry_state: "RetryCallState") -> None:


def before_sleep_log(
logger: "logging.Logger",
logger: _utils.LoggerProtocol,
log_level: int,
exc_info: bool = False,
sec_format: str = "%.3g",
Expand Down