Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enable debug logging for python requests
import requests
import logging
import httplib
# Debug logging
httplib.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
req_log = logging.getLogger('requests.packages.urllib3')
req_log.setLevel(logging.DEBUG)
req_log.propagate = True
@kuznetsov-m

This comment has been minimized.

Copy link

@kuznetsov-m kuznetsov-m commented Feb 29, 2020

Hi, @Daenyth!
I found a slightly different way to enable debug logging the logger for requests on the stackoverflow. What do you think about this?

# source: https://stackoverflow.com/a/57325050/10504918

import requests
import logging
from http.client import HTTPConnection  # py3

log = logging.getLogger('urllib3')
log.setLevel(logging.DEBUG)

# logging from urllib3 to console
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
log.addHandler(ch)

# print statements from `http.client.HTTPConnection` to console/stdout
HTTPConnection.debuglevel = 1

requests.get('http://httpbin.org/')
@Daenyth

This comment has been minimized.

Copy link
Owner Author

@Daenyth Daenyth commented Mar 2, 2020

I'm not up to date on python stuff these days, so I don't really know.

@rca

This comment has been minimized.

Copy link

@rca rca commented Jun 19, 2020

The HTTPConnection.debuglevel = 1 statement is magic; thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment