Marjan Moderc
Marjan Moderc

Reputation: 2849

Grouping '.format()'-style logging messages in Sentry

I use SentryHandler from raven.handlers.logging to track any logs of higher level within Sentry. My logging messages are dynamically populated with custom content with .format(), so the text message itself doesn't necessarily always have the same content. For example:

import logging
from raven.handlers.logging import SentryHandler
from raven.conf import setup_logging

# Create a "basic" logger
logger = logging.getLogger("root")

# Create a Sentry logger handler
sh = SentryHandler("https://******@sentry.io/******")
sh.setLevel(logging.WARNING)
setup_logging(sh)

# Send the desired message to Sentry via logger
if SomeInteresetingWarning():
    logger.warning("{} missing files in {} directiories!".format(num_files,num_dirs))

All good, only this causes every unique message to be considered as a unique Warning, which of course isn't true.

There is a nice QA covering this very problem on GitHub, but the solution provided there only applies to the strings, formatted with old-fashioned %s-style.

Does anybody know how to implement proper Sentry message grouping (aggregating) without having to redesign string formatting from format() back to %s placeholders?

Upvotes: 6

Views: 1522

Answers (1)

palkeo
palkeo

Reputation: 112

Now you can: https://docs.python.org/3/howto/logging-cookbook.html#use-of-alternative-formatting-styles

The gist is to use a formatter with style="{".

Upvotes: 0

Related Questions