Fix logger name misattribution by reading caller info from LogRecord - #678
Merged
Conversation
Contributor
Author
|
@GianlucaFicarelli this is an alternative fix for the logging issue. Please let me know which one makes more sense to apply. |
pgetta
marked this pull request as ready for review
July 22, 2026 11:16
Collaborator
There was a problem hiding this comment.
Thanks Pavlo, this PR looks more generic than #677 so I think we can go with this.
The new behaviour for name described in the PR description looks good to me, maybe even clearer than the previous one.
I just commented about the tests and the global effect of sentry initialization.
Codecov Report✅ All modified and coverable lines are covered by tests.
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
GianlucaFicarelli
approved these changes
Jul 23, 2026
Contributor
Author
|
Thank you Gianluca |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Since the Sentry integration (#667), every log emitted by third-party libraries through stdlib
logging(httpx, uvicorn, ...) is attributed tosentry_sdk.integrations.logginginstead of the real caller:{"time":"...","level":"INFO","name":"sentry_sdk.integrations.logging","message":"HTTP Request: GET .../userinfo \"HTTP/1.1 200 OK\"","extra":{...}}Cause
sentry-sdk's default
LoggingIntegrationmonkeypatcheslogging.Logger.callHandlers, inserting asentry_sdkframe into the call stack.InterceptHandler.emitre-derives the caller by walking frames and skipping only frames from theloggingmodule, so the walk now stops at Sentry's wrapper frame, and loguru derivesname/function/linefrom it. Same upstream report: Delgan/loguru#509.Fix
Stop walking frames altogether.
logging.Logger._logstampsrecord.name,record.funcName, andrecord.linenoviafindCaller()before any handler — patched or not — runs, soemitnow reads the caller off theLogRecordand applies it withL.patch(...):sentry_sdk.*.configure_logging()andinit_sentry().The request-context patcher from
configure_loggingis unaffected: loguru applies the core patcher first and.patch()composes on top of it.Tests
tests/test_logger.pyadds regression coverage. Per review feedback, the tests do not callsentry_sdk.init(itscallHandlersmonkeypatch is global and never reverted, and sentry is already covered bytest_sentry.py); instead, an autouse fixture wrapslogging.Logger.callHandlerswith an extra frame via pytest'smonkeypatch, simulating exactly what sentry'sLoggingIntegration.setup_oncedoes. The simulated patch reproduces the misattribution under the old frame-walking implementation, so the tests fail on pre-fix code.Alternative to #677 (closed in favor of this PR)
#677 fixed the same bug by extending the frame walk to also skip
sentry_sdk.*frames. The tradeoff that led to picking this PR:name= calling module's__name__, e.g.httpx._client) but hardcodes one library into the frame walk; any other library that wraps the handler chain reproduces the bug and needs another clause.nameto the stdlib logger name (e.g.httpxinstead ofhttpx._client). The two coincide for the commonlogging.getLogger(__name__)idiom; the logger name is also the key used inLOG_STANDARD_LOGGERand log filtering. If you have saved log queries or alerts matching exact module-levelnamevalues, they may need updating.