0

Problem:

I am trying to call a static method from a class method which throws an error. What is the right way to call that method in Python 3.x?

NameError: name 'strip_sensitive_data' is not defined

Code:

import sentry_sdk
from scrapy.exceptions import NotConfigured

class SentryLogging(object):
    """
    Send exceptions and errors to Sentry.
    """

    def strip_sensitive_data(event, hint):
        if event['level'] == 'error':
            return event
        else:
            return None

    @classmethod
    def from_crawler(cls, crawler):
        sentry_dsn = crawler.settings.get('SENTRY_DSN', None)
        environment = crawler.settings.get('ENVIRONMENT', None)
        if sentry_dsn is None:
            raise NotConfigured
        # instantiate the extension object
        ext = cls()
        # instantiate
        sentry_sdk.init(
            sentry_dsn,
            traces_sample_rate=1.0,
            environment= environment,
            before_send=strip_sensitive_data    
        )
        # return the extension object
        return ext
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
merlin
  • 2,717
  • 3
  • 29
  • 59
  • 3
    Move it out of your class, or use `SentryLogging.strip_sensitive_data`. Consider also `@staticmethod`. – khelwood Dec 16 '20 at 18:04
  • [This post](https://stackoverflow.com/a/47088589/13478738) may be helpful. It sounds very similar to your question. – Onelesscar Dec 16 '20 at 18:06
  • 1
    Maybe you meant `cls.strip_sensitive_data`? – Tomerikoo Dec 16 '20 at 18:06
  • 1
    (1) Add the `@staticmethod` decorator to `strip_sensitive_data` (2) You can call it from the class, e.g. `cls.strip_sensitive_data(...)`, or from outside of the class method you can do `SentryLogging.strip_sensitive_data(...)`, but add the missing decorator first. – Tom Karzes Dec 16 '20 at 18:08
  • Thank you all! That seemed to have done the trick. – merlin Dec 16 '20 at 18:11

0 Answers0