Skip to content

Instantly share code, notes, and snippets.

@sv0
Created May 14, 2018 10:45
Show Gist options
  • Save sv0/e8510a2f8db5c925a206703543d2279f to your computer and use it in GitHub Desktop.
Save sv0/e8510a2f8db5c925a206703543d2279f to your computer and use it in GitHub Desktop.
logging HTTP requests
import logging
import contextlib
try:
from http.client import HTTPConnection # py3 # noqa
except ImportError:
from httplib import HTTPConnection # py2 # noqa
# logger = logging.getLogger(__name__)
def debug_requests_on():
"""Switches on logging of the requests module"""
HTTPConnection.debuglevel = 3
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def debug_requests_off():
"""Switches off logging of the requests module, might be some side-effects
"""
HTTPConnection.debuglevel = 0
root_logger = logging.getLogger()
root_logger.setLevel(logging.WARNING)
root_logger.handlers = []
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.WARNING)
requests_log.propagate = False
@contextlib.contextmanager
def debug_requests():
"""Use with 'with'!"""
debug_requests_on()
yield
debug_requests_off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment