您可以从 Python Twilio 记录器中过滤掉个人详细信息吗?
默认情况下,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来您可以 申请使用记录器配置和继承自
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.
以下是如何在 Python 库中配置 Twilio 记录器以使用自定义格式化程序。为了测试这一点,我使用了您的格式化程序并对其进行了修改以编辑 Twilio 帐户、消息服务和电话号码。
发送短信后的示例日志:
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.
Example log after sending an SMS: