如何禁用请求库中的日志消息? [英] How do I disable log messages from the Requests library?

查看:83
本文介绍了如何禁用请求库中的日志消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,请求 python库将日志消息写到控制台,其方式如下:

By default, the Requests python library writes log messages to the console, along the lines of:

Starting new HTTP connection (1): example.com
http://example.com:80 "GET / HTTP/1.1" 200 606

我通常对这些消息不感兴趣,因此想禁用它们.使这些消息静音或降低请求的冗长程度的最佳方法是什么?

I'm usually not interested in these messages, and would like to disable them. What would be the best way to silence those messages or decrease Requests' verbosity?

推荐答案

我发现了如何配置请求的日志记录级别,这是通过标准的

I found out how to configure requests's logging level, it's done via the standard logging module. I decided to configure it to not log messages unless they are at least warnings:

import logging

logging.getLogger("requests").setLevel(logging.WARNING)

如果您也希望将此设置应用于urllib3库(通常由请求使用),请添加以下内容:

If you wish to apply this setting for the urllib3 library (typically used by requests) too, add the following:

logging.getLogger("urllib3").setLevel(logging.WARNING)

这篇关于如何禁用请求库中的日志消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆