Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Authors: Sylvain MARIE <sylvain.marie@se.com>
2# + All contributors to <https://github.com/smarie/python-azureml-client>
3#
4# License: 3-clause BSD, <https://github.com/smarie/python-azureml-client/blob/master/LICENSE>
5"""
6From https://stackoverflow.com/a/24588289/7262247.
7"""
8import logging
9from contextlib import contextmanager
11try:
12 from http.client import HTTPConnection # py3
13except ImportError:
14 from httplib import HTTPConnection # py2
17def debug_requests_on():
18 """
19 Switches on logging of the requests module.
20 """
21 HTTPConnection.debuglevel = 1
23 # logging.basicConfig()
24 # logging.getLogger().setLevel(logging.DEBUG)
25 requests_log = logging.getLogger("requests.packages.urllib3")
26 requests_log.setLevel(logging.DEBUG)
27 # if print_to_std_out:
28 # ch = logging.StreamHandler(sys.stdout)
29 # requests_log.addHandler(ch)
30 # requests_log.propagate = True
33def debug_requests_off():
34 """
35 Switches off logging of the requests module, might be some side-effects
36 """
37 HTTPConnection.debuglevel = 0
39 # root_logger = logging.getLogger()
40 # root_logger.setLevel(logging.WARNING)
41 # root_logger.handlers = []
42 requests_log = logging.getLogger("requests.packages.urllib3")
43 requests_log.setLevel(logging.WARNING)
44 # requests_log.propagate = False
47@contextmanager
48def debug_requests():
49 """
50 Context manager to temporarily enable debug mode on requests.
51 """
52 debug_requests_on()
53 yield
54 debug_requests_off()