Django:禁用python请求库记录

时间:2016-05-02 14:02:10

标签: python django logging python-requests

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

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或请求库时,我都会收到此日志记录。我已经尝试使用此设置删除它,并使用logging.disable(...)和logging.getLogger(" urllib3")。setLevel(logging.WARNING)

1 个答案:

答案 0 :(得分:1)

对于那些想要忽略这个相同范围的人,将以下行添加到文件的开头可以正常工作(虽然不是最佳解决方案):

httplib.HTTPConnection.debuglevel = 0