Skip to content

Instantly share code, notes, and snippets.

@Daenyth
Created August 27, 2015 14:35
Show Gist options
  • Star 58 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save Daenyth/b57f8522b388e66fcf3b to your computer and use it in GitHub Desktop.
Save Daenyth/b57f8522b388e66fcf3b to your computer and use it in GitHub Desktop.
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
Copy link

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
Copy link
Author

Daenyth commented Mar 2, 2020

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

@rca
Copy link

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