Python

How to Configure the Log Messages in Python Requests Library

In this post, we will discuss how to enable or disable the logging when using the requests library in Python. Although you will rarely need to enable the logging when using requests, it can be useful when debugging your application or the target resource.

Let ‘s us explore.

Requests Enable Logging

To enable the logging in of requests, add the following line:

import logging
logging.getLogger(“requests”).setLevel(logging.DEBUG)

The given entry should enable the log level to DEBUG which allows you to view the debug messages from the requests library.

An example is as shown in the following illustration:

>>> import logging
>>> logging.getLogger("requests").setLevel(logging.DEBUG)
>>> from http.client import HTTPConnection
>>> HTTPConnection.debuglevel = 1
>>> import requests
>>> requests.get("https://linuxhint.com")

Running the given code should return a very verbose message of every instruction to the target URL.

An example output is as shown:

send: b'GET / HTTP/1.1\r\nHost: linuxhint.com\r\nUser-Agent: python-requests/2.28.1\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: Mon, 19 Sep 2022 11:52:01 GMT
header: Content-Type: text/html; charset=UTF-8
header: Transfer-Encoding: chunked
header: Connection: keep-alive
header: vary: Accept-Encoding
header: set-cookie: ppwp_wp_session=3c0dcc6983321f9444f94a84a9d3d4a0%7C%7C1663590121%7C%7C1663589761; expires=Mon, 19-Sep-2022 12:22:01 GMT; Max-Age=1800; path=/
header: expires: Wed, 11 Jan 1984 05:00:00 GMT
header: Cache-Control: no-cache, must-revalidate, max-age=0, no-store
header: link: <https://linuxhint.com/wp-json/>; rel="https://api.w.org/"
header: link: <https://linuxhint.com/wp-json/wp/v2/pages/238>; rel="alternate"; type="application/json"
header: link: <https://linuxhint.com/>; rel=shortlink
header: x-powered-by: centminmod
header: x-hosted-by: BigScoots
header: content-security-policy: block-all-mixed-content;
header: x-xss-protection: 1; mode=block
header: x-content-type-options: nosniff
header: CF-Cache-Status: DYNAMIC
header: Report-To: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=%2BJ9DRCCFfwaUM%2Fm%2F8xgPZ0ZjrUAFxQsWrHcw%2BcfaatFXNj3PLZgFq5Z1gFY
dAGttdWQT%2FyInfOJmOw0Yh1KxbyxLInnBgXJhfDXfYK9NMPpiii1cL8mW31PMLn4oeAPX"
}],"group":"cf-nel","max_age":604800}
header: NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
header: Server: cloudflare
header: CF-RAY: 74d21cda5c69d739-DAR
header: Content-Encoding: gzip
header: alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
<Response [200]>

The given illustration shows the log messages when making a request to the target URL.

Requests Disable Logging

You can disable the logging for the requests by setting the log level to 0 as shown:

>>> HTTPConnection.debuglevel = 0

We can then make the request to a target resource as:

>>> requests.get("https://linuxhint.com")
<Response [200]>

We can see that the log level is disabled.

Conclusion

In this post, we discussed how to enable and disable the logging in the Python requests library using the Python logger.

Happy coding!

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list