Skip to content

Fix logger name misattributed to sentry_sdk.integrations.logging - #677

Closed
pgetta wants to merge 1 commit into
mainfrom
fix-intercept-handler-sentry-frames
Closed

Fix logger name misattributed to sentry_sdk.integrations.logging#677
pgetta wants to merge 1 commit into
mainfrom
fix-intercept-handler-sentry-frames

Conversation

@pgetta

@pgetta pgetta commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Problem

Since the Sentry integration (#667), every log emitted by third-party libraries through stdlib logging is reported with "name":"sentry_sdk.integrations.logging" instead of the originating module:

{"time":"...","level":"INFO","name":"sentry_sdk.integrations.logging","message":"HTTP Request: GET https://.../userinfo \"HTTP/1.1 200 OK\"",...}

Cause

Sentry's default LoggingIntegration monkeypatches logging.Logger.callHandlers, inserting a sentry_sdk frame into the call stack between the stdlib logging frames. InterceptHandler.emit locates the original caller by walking frames and skipping only frames from the logging module, so the walk now stops at Sentry's wrapper. Loguru then derives record["name"] (and function/line) from that frame instead of the real caller. Known issue with the loguru intercept recipe: Delgan/loguru#509.

Fix

Extend the frame walk to also skip frames whose module is sentry_sdk.*. Checked via the frame's __name__ so app/logger.py doesn't need to import sentry_sdk, preserving the init ordering documented in app/sentry.py.

Testing

  • New regression test in tests/test_logger.py: initializes sentry (asserting Logger.callHandlers is actually patched), routes a stdlib log through InterceptHandler, and asserts the loguru record is attributed to the calling module/function. Fails with 'sentry_sdk.integrations.logging' == 'tests.test_logger' without the fix.
  • Full suite passes (1068 tests).

Sentry's default LoggingIntegration patches logging.Logger.callHandlers,
inserting a sentry_sdk frame into the call stack. InterceptHandler's
frame walk stopped at that frame, so loguru attributed every intercepted
stdlib log (httpx, uvicorn, ...) to sentry_sdk.integrations.logging.
Skip sentry_sdk frames in the walk to restore correct caller attribution.
@pgetta pgetta self-assigned this Jul 22, 2026
@pgetta
pgetta marked this pull request as ready for review July 22, 2026 08:38
@pgetta
pgetta requested a review from GianlucaFicarelli July 22, 2026 08:38
@codecov

codecov Bot commented Jul 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Flag Coverage Δ
pytest 97.84% <100.00%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
app/logger.py 90.00% <100.00%> (+14.00%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@pgetta

pgetta commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

Closing in favour of #678

@pgetta pgetta closed this Jul 23, 2026
@pgetta
pgetta deleted the fix-intercept-handler-sentry-frames branch July 23, 2026 06:53
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.

1 participant