Created
May 14, 2018 10:45
-
-
Save sv0/e8510a2f8db5c925a206703543d2279f to your computer and use it in GitHub Desktop.
logging HTTP requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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