question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Grouping and JSON log messages

See original GitHub issue

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?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
boxyseancommented, Mar 6, 2020

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)
0reactions
antonpirkercommented, Mar 4, 2022

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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

8 Essential Tips for JSON logging - Papertrail
8 Essential Tips for JSON logging · Validate Your JSON · Create a Standard Schema · Include a Timestamp in Every Message ·...
Read more >
Structured logging - Google Cloud
Because JSON is more precise and versatile than text lines, you can use JSON objects to write multiline messages and add metadata. To...
Read more >
Parse JSON Formatted Logs - Sumo Logic Docs
The JSON operator allows you to extract values from JSON logs with most JSONPath expressions. See the supported JSONPath syntax elements below.
Read more >
JSON Logs with CloudWatch Logs Insights - In Plain English
Insights will automatically parse the content and create fields based on the JSON message. Insights can extract a maximum of 1000 log event ......
Read more >
Group Logs - Datadog Docs
With pattern aggregation, logs that have a message with similar structures are grouped altogether. Optionally select one to three faceted fields to pre- ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found