【问题标题】:Django: Disable python Requests Library LoggingDjango:禁用 python 请求库日志记录
【发布时间】:2016-08-27 07:56:12
【问题描述】:

我正在尝试从请求库中删除日志记录。我尝试了网络上的所有内容,但我不断收到这样的垃圾邮件:

send: 'GET / HTTP/1.1\r\nHost: www.google.com\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.3.0 CPython/2.7.10 Darwin/15.2.0\r\n\r\n'
reply: 'HTTP/1.1 302 Found\r\n'
header: Cache-Control: private
header: Content-Type: text/html; charset=UTF-8
header: Location: https://www.google.pt/?gfe_rd=cr&ei=plwnV9OUHYas8wekio2ADw
header: Content-Length: 259
header: Date: Mon, 02 May 2016 13:56:54 GMT
send: 'GET /?gfe_rd=cr&ei=plwnV9OUHYas8wekio2ADw HTTP/1.1\r\nHost: www.google.pt\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nUser-Agent: python-requests/2.3.0 CPython/2.7.10 Darwin/15.2.0\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Mon, 02 May 2016 13:56:54 GMT
header: Expires: -1
header: Cache-Control: private, max-age=0
header: Content-Type: text/html; charset=ISO-8859-1
header: P3P: CP="This is not a P3P policy! See https://www.google.com/support/accounts/answer/151657?hl=en for more info."
header: Content-Encoding: gzip
header: Server: gws
header: X-XSS-Protection: 1; mode=block
header: X-Frame-Options: SAMEORIGIN
header: Set-Cookie: NID=79=OojMOTKzHw7ADN02S4j_nk-sRaWCeQ8P-JTKiCEarWlwIqzYnH2tP0GEVOov-Svl8Pn5-cMpv6sILvEKUaC0PayZe8PeAzN8uaqpTeDjJpka2KDjZyOSXig7bQBqw-mv; expires=Tue, 01-Nov-2016 13:56:54 GMT; path=/; domain=.google.pt; HttpOnly
header: Transfer-Encoding: chunked

而这一切都只是为了这行代码:requests.get('https://www.google.com', timeout=5)

我目前的设置是这样的:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'null': {
            'level': 'DEBUG',
            'class': 'django.utils.log.NullHandler',
        },
        'console': {
            # logging handler that outputs log messages to terminal
            'class': 'logging.StreamHandler',
            'level': 'WARNING', # message level to be written to console
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'django.db': {
            'level': 'DEBUG',
            'handlers': ['console'],
        },
        'boto': {
            'level': 'CRITICAL',
            'handlers': ['console'],
            'propagate': False,
        },
        'requests.packages.urllib3': {
            'level': 'WARNING',
            'handlers': ['null'],
            'propagate': False,
        },
    }
}

有没有办法删除这个垃圾邮件?每当我使用 boto s3 或 requests 库时,我都会收到此日志记录。我已经尝试使用此设置以及 logging.disable(...) 和 logging.getLogger("urllib3").setLevel(logging.WARNING)

删除它

【问题讨论】:

  • 看看答案here
  • 这至少在我使用的版本中没有帮助:requests==2.3.0。如果你调试它,你会在他们的代码中找到“if this.debuglevel>0: print (...)”=> 这意味着它的硬编码为 debuglevel > 0,它是打印而不是记录器。我在某处发现这是一个错误,可能会在较新的版本中得到修复

标签: python django logging python-requests


【解决方案1】:

对于那些想要忽略相同跨度的人,在文件开头添加以下行就可以了(虽然不是最佳解决方案):

httplib.HTTPConnection.debuglevel = 0 

【讨论】:

    猜你喜欢
    • 2016-07-11
    • 2012-10-08
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-23
    • 2014-07-16
    相关资源
    最近更新 更多