How to add tags to python logging

Here’s a snippet of sample code that I’m using to log in a python script.

handler = SentryHandler('')
logger = logging.getLogger(__name__)

logger.debug("This is a debug log message")

I’d like to add tags to this, but I don’t know how.

You can pass supported arguments via logging’s ‘extra’ parameter. You can also use the tags_context method on raven.Client.

Thank you for the reply, and sorry for my ignorance, but ‘tags’ (or ‘tag’) does not seem to be a supported parameter to extra.

logger.debug("This message should contain some extra info", extra={ tags : 'test' })

gives me:
NameError: name 'tags' is not defined

if I change it to:
logger.debug("This message should contain some extra info", extra={ 'tag' : 'test' })

It goes through, but does not actually show as a ‘tag’ in Sentry, it just adds it to the extra data:
But it is not listed as a ‘tag’ on the right under ‘Tags’


A quick follow up.

According to this pull request:

(prepend with http:// … I’m not allowed to post another link… ugh)

I should be able to send tags with the syntax:
logger.debug('Hello', tags={'user': 'dexter'})

But when I try that I get:

Traceback (most recent call last):
  File "/Users/johnsturgeon/Code/greatfootballpool-v2/bin/", line 20, in <module>
    logger.debug('Hello', tags={'user': 'dexter'})
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/", line 1145, in debug
    Log 'msg % args' with severity 'DEBUG'.
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/", line 1153, in debug
    self._log(DEBUG, msg, args, **kwargs)
TypeError: _log() got an unexpected keyword argument 'tags'

Your first attempt was correct, but not valid python, it’s extra={‘tags’: {}}


Thank you… that did it :thumbsup: