Sentry-python: Grouping and JSON log messages

Created on 3 Jan 2020  路  5Comments  路  Source: getsentry/sentry-python

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?

question

Most helpful comment

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)

All 5 comments

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)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

mumumumu picture mumumumu  路  6Comments

hyperknot picture hyperknot  路  4Comments

miracle2k picture miracle2k  路  6Comments

stefanondisponibile picture stefanondisponibile  路  5Comments

tonal picture tonal  路  4Comments