Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't allow passing in loguru #420

Open
crizCraig opened this issue Oct 20, 2023 · 1 comment
Open

Don't allow passing in loguru #420

crizCraig opened this issue Oct 20, 2023 · 1 comment

Comments

@crizCraig
Copy link

Loguru is not api compatible with standard logging despite efforts in #294, so it may be best to raise an error or wrap the loguru logger with a standard logger when doing this

from loguru import logger
@retry(
    stop=stop_after_attempt(10),
    wait=wait_random_exponential(multiplier=1, max=10),
    reraise=True,
    before_sleep=before_sleep_log(logger, "ERROR"),  # type: ignore
    after=after_log(logger, "ERROR"),  # type: ignore
)

As it can cause a KeyError due to this

Here's a wrapping approach which may be the best thing for people to setup in accordance with their own logging needs. Not sure if @Delgan has a better approach though.

@Delgan
Copy link

Delgan commented Oct 27, 2023

Hi there.

As per the type-hints in the source code of tenacity, the logger parameter is clearly expecting a standard logging.Logger object:

def before_sleep_log(
logger: "logging.Logger",
log_level: int,
exc_info: bool = False,
) -> typing.Callable[["RetryCallState"], None]:
"""Before call strategy that logs to some logger the attempt."""

Consequently, users should not expect tenacity to be compatible with Loguru's logger, nor should tenacity developers necessarily care about that. The compatibility error should be raised by static linters such as Mypy.

In this situation, the most appropriate workaround is for users to define a standard logger wrapping Loguru's logger to bridge the gap between the two libraries. I added more details in Delgan/loguru#1008 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants