Skip to content

Make the structlog integration for Sentry logs #4417

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

Open
sector119 opened this issue May 26, 2025 · 4 comments
Open

Make the structlog integration for Sentry logs #4417

sector119 opened this issue May 26, 2025 · 4 comments

Comments

@sector119
Copy link

sector119 commented May 26, 2025

Problem Statement

structlog logs can be sent to Sentry via the existing LoggingIntegration; however, this does not preserve the structuring of the logs (e.g. if the logs have custom attributes set, these are included in the log message). See this example

Solution Brainstorm

Instead, create a dedicated structlog integration, which would add these logs to the log event as custom attributes.

@szokeasaurusrex
Copy link
Member

Hi @sector119, thank you for raising this issue.

While we would be open to considering adding a structlog integration, it is already possible to get structlog to work by calling structlog.stdlib.recreate_defaults() near the beginning of your application's lifecycle, ideally right after the sentry_sdk.init() call.

Calling structlog.stdlib.recreate_defaults() causes all of your structlog logs to be routed through Python's default logging library. That way, the Sentry SDK's logging integration can pick them up.

Here is an example of how you can get your structlog logs to show up in Sentry.

import sentry_sdk
import structlog


def main():
    sentry_sdk.init(
        dsn="your dsn here",
        _experiments={
            "enable_logs": True,
        },
    )
    structlog.stdlib.recreate_defaults()  # Add this call
    logger = structlog.get_logger()
    logger.info("Hello, world!")  # This gets logged in Sentry


if __name__ == "__main__":
    main()

Does this solution work for you? If yes, please close the issue. If not, please explain why this does not work, and we will use that information to inform our decision on whether to create a dedicated structlog integration.

@sector119
Copy link
Author

Does this solution work for you? If yes, please close the issue. If not, please explain why this does not work, and we will use that information to inform our decision on whether to create a dedicated structlog integration.

Thanks a lot, it works for me, but that is not what I exactly want to get.

for example I perform

logger.error("Account not found", person_id=person_id, street_id=street_id, building=building, apartment=apartment)

with structlog json formatter I get message like this, so I have message and arguments logged separately

{"person_id":2036273,"street_id":null,"building":null,"apartment":null,"event":"Account not found","level":"error","logger":"epsilon-xmlrpc","timestamp":"2025-05-27T00:05:19.820515","sentry_id":"f54adb5462254e0fb42111e3d3149452"}

I thought that with sentry logging I would get something similar, so I would be able to search by all that args

Right now I just have message with all args in the same line (

2025-05-27 00:01:20 [error ] Account not found [epsilon-xmlrpc] apartment=None building=None person_id=2036273 street_id=None

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 May 26, 2025
Copy link
Member

Ah okay, I understand now! Basically, you would like us to attach this information as attributes onto the log that we send to Sentry.

I believe this is a completely valid request and should be possible for us to implement, so I will add this to our backlog as a feature request.

@sector119
Copy link
Author

Ah okay, I understand now! Basically, you would like us to attach this information as attributes onto the log that we send to Sentry.

Yes, as attributes, hints or something similar that we can search by

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

No branches or pull requests

2 participants