Grouping and JSON log messages
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:

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?
Are you using structlog by chance? Please refer to #228.
I am not
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 ?
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.
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)
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 🥀
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!
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!