python requests logging

Python Requests Logging

If you are working with Python Requests module and want to debug your code or track your HTTP requests and responses, logging can be very helpful. You can use Python's built-in logging module to log the information you need.

Basic Logging Configuration

Here is a basic logging configuration that will log all requests and responses:


import logging
import requests

# Configure basic logging
logging.basicConfig(level=logging.DEBUG)

# Make a request
response = requests.get('http://www.example.com')

# Log the response content
logging.debug(response.content)
    

When you run this code, you will see the HTTP request and response information in the console:


DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): www.example.com
DEBUG:urllib3.connectionpool:http://www.example.com:80 "GET / HTTP/1.1" 200 None
DEBUG:root:b'\n\n\nExample Domain\n\n\n\n\n\nbody {\n    background-color: #f0f0f2;\n    margin: 0;\n    padding: 0;\n    font-family: \"Open Sans\", \"Helvetica Neue\", Helvetica, Arial, sans-serif;\n    \n}\ndiv {\n    width: 600px;\n    margin: 5em auto;\n    padding: 50px;\n    background-color: #fff;\n    border-radius: 1em;\n}\na:link, a:visited {\n    color: #38488f;\n    text-decoration: none;\n}\na:hover, a:active {\n    color: #f00;\n}\n\n@media (max-width: 700px) {\n    div {\n        width: auto;\n        margin: 0 auto;\n    }\n}\n    \n\n\n\n\nExample Domain\nThis domain is established to be used for illustrative examples in documents. You may use this\n    domain in examples without prior coordination or asking for permission.\nMore information...\n\n\n\n'
    

Advanced Logging Configuration

You can also configure logging to only log certain types of requests or responses, or to log to a file instead of the console. Here is an example of more advanced logging configuration:


import logging
import requests

# Configure logging
logger = logging.getLogger('requests')
logger.setLevel(logging.DEBUG)

# Create a file handler
handler = logging.FileHandler('requests.log')
handler.setLevel(logging.DEBUG)

# Create a formatter and add it to the handler
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

# Add the handler to the logger
logger.addHandler(handler)

# Make a request
response = requests.get('http://www.example.com')

# Log the response content
logger.debug(response.content)
    

This configuration will log only the requests and responses that have a level of DEBUG or higher, and will write them to a file called "requests.log".

Conclusion

Python Requests module is a powerful tool for making HTTP requests, and logging can help you debug your code and track your requests and responses. With Python's built-in logging module, you can configure logging to meet your specific needs.