sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

Grouping and JSON log messages

Open synic opened this issue 5 years ago • 7 comments

We've recently switched to a new logging format, which is in JSON instead of a simple string.

This has had a detrimental affect on our sentry captures, as it is unable to group the log messages by title. Instead, it looks like this:

screenshot

The message we are sending looks like the following:

{"code": "INT33443",
 "message": {
    "message": "Error syncing your account",
    "type": "public",
    "company_id": 3
 }
}

Is there a way I can get sentry to display message->message as the title and group by that?

synic avatar Jan 03 '20 21:01 synic

Are you using structlog by chance? Please refer to #228.

untitaker avatar Jan 07 '20 08:01 untitaker

I am not

synic avatar Jan 07 '20 12:01 synic

I got the same kind of issue, without using structlog, my guess would be that we need to override for our use case the EventHandler that is used by LoggingIntegration ?

allan-simon avatar Feb 12 '20 22:02 allan-simon

Could you prepare a standalone example that exhibits this issue? We are using this log handler ourselves in production and do not have this issue.

untitaker avatar Feb 12 '20 22:02 untitaker

As @allan-simon mentioned, you can update the EventHandler and use a custom logging.Formatter.

This works for me:

class BareMessageFromJSONFormatter(logging.Formatter):
    def format(self, record):
        """
        Oddly enough, return result is not used, so update the record
        message in-place. I'm not sure if this is intentional by
        Sentry.

        See: https://github.com/getsentry/sentry-python/blob/41120009fa7d6cb88d9219cb20874c9dd705639d/sentry_sdk/integrations/logging.py#L227
        """
        if isinstance(record.msg, dict):
            record.msg = record.msg.get('msg')

And used:

        sentry_sdk.init(
            dsn=sentry_dsn,
            integrations=[
                sentry_sdk.integrations.logging.LoggingIntegration(
                    event_level=None,
                    level=None,
                )
            ],
            default_integrations=True,
        )

        event_handler = sentry_sdk.integrations.logging.EventHandler(level=logging.ERROR)
        event_handler.setFormatter(BareMessageFromJSONFormatter())
        logging.getLogger().addHandler(event_handler)

        breadcrumb_handler = sentry_sdk.integrations.logging.BreadcrumbHandler(level=logging.INFO)
        breadcrumb_handler.setFormatter(BareMessageFromJSONFormatter())
        logging.getLogger().addHandler(breadcrumb_handler)

boxysean avatar Mar 06 '20 18:03 boxysean

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

github-actions[bot] avatar Dec 23 '21 15:12 github-actions[bot]

This issue is related to some other issues (I tagged all of them with the "Integration: Logging" label) I put it in the internal backlog with low priority. Any help here is greatly appreciated!

antonpirker avatar Mar 04 '22 09:03 antonpirker

As there has been a workaround posted above I will close this issue.

If there is still a unsolved problem with structured logs and grouping them please create a new issue, thanks!

antonpirker avatar Dec 10 '24 12:12 antonpirker