繁体   English   中英

python3.7请求:记录到文件:不记录所有内容

[英]python3.7 requests: logging to a file: Does not log everything

我在用

$ python --version                                                                                                                                                                      
Python 3.7.3

我正在尝试使用以下代码将请求的 output 记录到文件中

import requests
import logging

import http.client as http_client
http_client.HTTPConnection.debuglevel = 1

# You must initialize logging, otherwise you'll not see debug output.
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True

handler = logging.FileHandler('test2.log',mode='a')
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
handler.setLevel(logging.DEBUG)  

requests_log.addHandler(handler)

requests.get('https://httpbin.org/headers')

我在test2.log中看到的是

2021-03-07 17:50:22,312 - urllib3.connectionpool - DEBUG - Starting new HTTPS connection (1): httpbin.org:443
2021-03-07 17:50:22,376 - urllib3.connectionpool - DEBUG - https://httpbin.org:443 "GET /headers HTTP/1.1" 200 225

为什么我看不到日志中控制台中显示的其他output,即如下

DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): httpbin.org:443
send: b'GET /headers HTTP/1.1\r\nHost: httpbin.org\r\nUser-Agent: python-requests/2.24.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\n\r\n'
reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Sun, 07 Mar 2021 12:20:21 GMT
header: Content-Type: application/json
header: Content-Length: 225
header: Connection: keep-alive
header: Server: gunicorn/19.9.0
header: Access-Control-Allow-Origin: *
header: Access-Control-Allow-Credentials: true
DEBUG:urllib3.connectionpool:https://httpbin.org:443 "GET /headers HTTP/1.1" 200 225

如何将标头数据也放入日志

您是否尝试过修补http.client模块的打印 function ? 不幸的是,它使用打印而不是 Python 日志记录模块,因此出现了这种行为。

在您的请求生效之前添加以下代码:

def print_to_log(*args):
    requests_log.debug(" ".join(args)) 
http_client.print = print_to_log

相关答案( 将来自 python-requests 模块Python HTTP 请求和调试级别日志记录到日志文件的所有请求记录下来)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM