您可以从 Python Twilio 记录器中过滤掉个人详细信息吗?

发布于 2025-01-14 15:10:47 字数 1085 浏览 1 评论 0原文

默认情况下,Twilio Python 客户端似乎会注销个人信息(电子邮件和号码)。我知道您可以像这样访问记录器twilio_logger =logging.getLogger('twilio.http_client')

我们是否可以进行简单的设置更改来过滤掉这些个人信息,无论是直接通过库还是通过更改记录器?

注意:我尝试通过创建自定义格式化程序来做到这一点:

class SensitiveDataFormatter(logging.Formatter):
    """Formatter that removes sensitive information in urls."""
    @staticmethod
    def _filter(s):
        print('filter')
        return re.sub(r'([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})', r'[email_redacted]', s)

    def format(self, record):
        original = logging.Formatter.format(self, record)
        return self._filter(original)


LOGGING = {
    'version': 1,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'sensitive'
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO'
    },
    'formatters': {
        'sensitive': {
            '()': 'my_project.settings.logging.sensitive_data_formatter.SensitiveDataFormatter'
        }
    }
}

这在一般情况下有效,但是我正在努力将其应用到 Twilio

By default the Twilio Python client seems to log out personal information (emails and numbers). I know you can access the logger like this twilio_logger = logging.getLogger('twilio.http_client')

Is there a simple setting change we can make to filter out this personal information, either directly through the library or by changing the logger?

NOTE: I have attempted to do this by creating a custom formatter:

class SensitiveDataFormatter(logging.Formatter):
    """Formatter that removes sensitive information in urls."""
    @staticmethod
    def _filter(s):
        print('filter')
        return re.sub(r'([A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,})', r'[email_redacted]', s)

    def format(self, record):
        original = logging.Formatter.format(self, record)
        return self._filter(original)


LOGGING = {
    'version': 1,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'sensitive'
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'INFO'
    },
    'formatters': {
        'sensitive': {
            '()': 'my_project.settings.logging.sensitive_data_formatter.SensitiveDataFormatter'
        }
    }
}

This works in the general case, however I'm struggling to apply it to Twilio

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半岛未凉 2025-01-21 15:10:48

看来您可以 申请使用记录器配置和继承自 logging.Filter 的自定义类过滤 Python 记录器。

我不是 Python 开发人员,因此请您参阅这篇博文 是我回答这个问题的最佳方式。我会注意到这篇文章似乎是从 2014 年开始的,所以可能需要 Python 3 的更新,但是 Python 日志记录文档 也非常广泛,因此应该对您有所帮助。

It seems that you can apply filters to Python loggers using logger config and a custom class that inherits from logging.Filter.

I'm not a Python developer, so pointing you to this blog post is the best way I can answer this question. I will note that the post appears to be from 2014, so may need updates for Python 3, but the Python logging documentation is also pretty extensive, so should help you out here.

日记撕了你也走了 2025-01-21 15:10:48

以下是如何在 Python 库中配置 Twilio 记录器以使用自定义格式化程序。为了测试这一点,我使用了您的格式化程序并对其进行了修改以编辑 Twilio 帐户、消息服务和电话号码。

client = Client(account_sid, auth_token)

class SensitiveDataFormatter(logging.Formatter):
    @staticmethod
    def _filter(s):
        filters = [
            [r'(AC[a-f0-9]+)', '[account_redacted]'],
            [r'(MG[a-f0-9]+)', '[messaging_service_redacted]'],
            [r'(\+[0-9]+)', '[phone_redacted]'],
        ]
        for f in filters:
            s = re.sub(f[0], f[1], s)
        return s

    def format(self, record):
        original = logging.Formatter.format(self, record)
        return self._filter(original)

# create a logging handler with the custom formatter attached
handler = logging.StreamHandler()
handler.setFormatter(SensitiveDataFormatter())

# add the stream handler to the Twilio logger
client.http_client.logger.addHandler(handler)
client.http_client.logger.setLevel(logging.INFO)

发送短信后的示例日志:

POST Request: https://api.twilio.com/2010-04-01/Accounts/[account_redacted]/Messages.json
PAYLOAD: {'To': '[phone_redacted]', 'From': '[messaging_service_redacted]', 'Body': 'Friendly reminder that you have an appointment with us next week.'}
POST Response: 201 {"sid": "SM4428eb430949461d9bd58d44da1dd999", "date_created": "Thu, 19 May 2022 08:57:49 [phone_redacted]", "date_updated": "Thu, 19 May 2022 08:57:49 [phone_redacted]", "date_sent": null, "account_sid": "[account_redacted]", "to": "[phone_redacted]", "from": null, "messaging_service_sid": "[messaging_service_redacted]", "body": "Friendly reminder that you have an appointment with us next week.", "status": "accepted", "num_segments": "0", "num_media": "0", "direction": "outbound-api", "api_version": "2010-04-01", "price": null, "price_unit": null, "error_code": null, "error_message": null, "uri": "/2010-04-01/Accounts/[account_redacted]/Messages/SM4428eb430949461d9bd58d44da1dd999.json", "subresource_uris": {"media": "/2010-04-01/Accounts/[account_redacted]/Messages/SM4428eb430949461d9bd58d44da1dd999/Media.json"}}

Here is how you can configure the Twilio logger in the Python library to use a custom formatter. To test this I took your formatter and modified it to redact Twilio accounts, messaging services and phone numbers.

client = Client(account_sid, auth_token)

class SensitiveDataFormatter(logging.Formatter):
    @staticmethod
    def _filter(s):
        filters = [
            [r'(AC[a-f0-9]+)', '[account_redacted]'],
            [r'(MG[a-f0-9]+)', '[messaging_service_redacted]'],
            [r'(\+[0-9]+)', '[phone_redacted]'],
        ]
        for f in filters:
            s = re.sub(f[0], f[1], s)
        return s

    def format(self, record):
        original = logging.Formatter.format(self, record)
        return self._filter(original)

# create a logging handler with the custom formatter attached
handler = logging.StreamHandler()
handler.setFormatter(SensitiveDataFormatter())

# add the stream handler to the Twilio logger
client.http_client.logger.addHandler(handler)
client.http_client.logger.setLevel(logging.INFO)

Example log after sending an SMS:

POST Request: https://api.twilio.com/2010-04-01/Accounts/[account_redacted]/Messages.json
PAYLOAD: {'To': '[phone_redacted]', 'From': '[messaging_service_redacted]', 'Body': 'Friendly reminder that you have an appointment with us next week.'}
POST Response: 201 {"sid": "SM4428eb430949461d9bd58d44da1dd999", "date_created": "Thu, 19 May 2022 08:57:49 [phone_redacted]", "date_updated": "Thu, 19 May 2022 08:57:49 [phone_redacted]", "date_sent": null, "account_sid": "[account_redacted]", "to": "[phone_redacted]", "from": null, "messaging_service_sid": "[messaging_service_redacted]", "body": "Friendly reminder that you have an appointment with us next week.", "status": "accepted", "num_segments": "0", "num_media": "0", "direction": "outbound-api", "api_version": "2010-04-01", "price": null, "price_unit": null, "error_code": null, "error_message": null, "uri": "/2010-04-01/Accounts/[account_redacted]/Messages/SM4428eb430949461d9bd58d44da1dd999.json", "subresource_uris": {"media": "/2010-04-01/Accounts/[account_redacted]/Messages/SM4428eb430949461d9bd58d44da1dd999/Media.json"}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文