Skip to content

fix: format traceback inline for loguru compatibility#622

Merged
mergify[bot] merged 1 commit intomainfrom
devs/jd/fix/loguru-exc-info/Icc145539d443c83f09266e25b9819bd2516aeddc
Mar 5, 2026
Merged

fix: format traceback inline for loguru compatibility#622
mergify[bot] merged 1 commit intomainfrom
devs/jd/fix/loguru-exc-info/Icc145539d443c83f09266e25b9819bd2516aeddc

Conversation

@jd
Copy link
Copy Markdown
Owner

@jd jd commented Mar 3, 2026

Stop passing exc_info= as a kwarg to logger.log(). Standard
logging.Logger treats this specially (appends traceback), but loguru
interprets all kwargs as message format parameters, causing a KeyError.

Instead, format the traceback into the message string directly using
traceback.format_exception(). This works with all loggers that
implement LoggerProtocol (logging, loguru, structlog, etc.).

Also tighten LoggerProtocol.log() by removing **kwargs since we
no longer pass any keyword arguments.

Fixes #420

Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com

Stop passing `exc_info=` as a kwarg to `logger.log()`. Standard
`logging.Logger` treats this specially (appends traceback), but loguru
interprets all kwargs as message format parameters, causing a KeyError.

Instead, format the traceback into the message string directly using
`traceback.format_exception()`. This works with all loggers that
implement `LoggerProtocol` (logging, loguru, structlog, etc.).

Also tighten `LoggerProtocol.log()` by removing `**kwargs` since we
no longer pass any keyword arguments.

Fixes #420

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Change-Id: Icc145539d443c83f09266e25b9819bd2516aeddc
@jd jd mentioned this pull request Mar 3, 2026
@Delgan
Copy link
Copy Markdown

Delgan commented Mar 4, 2026

Hi @jd. Thanks for considering updating tenacity to support loguru.

The change looks good. I tested it locally and I confirmed that the @retry decorator can now be used successfully with Loguru's logger.

@jd jd marked this pull request as ready for review March 5, 2026 07:48
Copilot AI review requested due to automatic review settings March 5, 2026 07:48
mergify bot added a commit that referenced this pull request Mar 5, 2026
@mergify mergify bot merged commit 21189c7 into main Mar 5, 2026
11 checks passed
@mergify mergify bot deleted the devs/jd/fix/loguru-exc-info/Icc145539d443c83f09266e25b9819bd2516aeddc branch March 5, 2026 07:49
@mergify
Copy link
Copy Markdown
Contributor

mergify bot commented Mar 5, 2026

Merge Queue Status

Rule: default


This pull request spent 11 seconds in the queue, including 1 second running CI.

Required conditions to merge

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses issue #420 by fixing a KeyError that occurred when using loguru (or similar loggers) with before_sleep_log(). The root cause was that the old code passed exc_info= as a keyword argument to logger.log(), which standard logging.Logger handles specially (appending a traceback), but loguru treats as a message format parameter, causing a KeyError. The fix embeds the traceback directly into the message string using traceback.format_exception(), making the behavior portable across all loggers conforming to LoggerProtocol.

Changes:

  • The traceback is now formatted inline into the log message string using traceback.format_exception() instead of being passed as exc_info= kwarg to logger.log()
  • The local_exc_info intermediate variable and the exc_info= kwarg in logger.log() calls are removed
  • LoggerProtocol.log() has **kwargs removed from its signature

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
tenacity/before_sleep.py Replaces exc_info= kwarg in logger.log() with inline traceback formatting into the message string
tenacity/_utils.py Removes **kwargs from LoggerProtocol.log() since it is no longer called with keyword arguments

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

)

if exc_info and retry_state.outcome.failed:
ex = retry_state.outcome.exception()
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ex variable is already assigned at line 46 when retry_state.outcome.failed is True. Since the guard condition on line 58 (if exc_info and retry_state.outcome.failed) ensures we are in the same failed branch, the re-assignment ex = retry_state.outcome.exception() on line 59 is a redundant duplicate call. The previously set ex from line 46 can be reused directly, eliminating the extra method call.

Suggested change
ex = retry_state.outcome.exception()

Copilot uses AI. Check for mistakes.
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: ...
Copy link

Copilot AI Mar 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing **kwargs from LoggerProtocol.log() is a breaking API change for any users who have custom logger implementations typed against this protocol. The previous signature was def log(self, level: int, msg: str, *args: Any, **kwargs: Any) -> Any, which matched the broader logging.Logger.log signature that accepts stacklevel, extra, etc. as keyword arguments. Users who previously passed such keyword arguments through a LoggerProtocol-typed reference will now get a static type error. Consider whether a more conservative change (keeping **kwargs in the protocol even if tenacity itself no longer passes kwargs) would be preferable for backward compatibility.

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

Copilot uses AI. Check for mistakes.
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

Successfully merging this pull request may close these issues.

Don't allow passing in loguru

3 participants