How To Log All Requests From The Python Request Library?

Better Stack Team
Updated on October 6, 2023

If you are new to logging in Python, please feel free to start with our Introduction to Python logging to get started smoothly. Otherwise, here is how to log all requests from the python request library:

You need to use urllib3 logger and set the log level to DEBUG:

 
log = logging.getLogger('urllib3')

log.setLevel(logging.DEBUG)

To maximise the message you can get, set HTTPConnection.debuglevel to 1:

 
from http.client import HTTPConnection
HTTPConnection.debuglevel = 1

By using the additional method, that cannot be captured by logging, you need to settle for console output.

If you would like to have more options when it comes to logging, you can explore third-party frameworks like Loguru. Here is our Guide for how to get started with this most popular framework for Python.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github