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 """
6 From https://stackoverflow.com/a/24588289/7262247.
7 """
8 import logging
9 from contextlib import contextmanager
10
11 try:
-
E261
At least two spaces before inline comment
12 from http.client import HTTPConnection # py3
13 except ImportError:
-
E261
At least two spaces before inline comment
14 from httplib import HTTPConnection # py2
15
16
17 def debug_requests_on():
18 """
19 Switches on logging of the requests module.
20 """
21 HTTPConnection.debuglevel = 1
22
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
31
32
33 def debug_requests_off():
34 """
35 Switches off logging of the requests module, might be some side-effects
36 """
37 HTTPConnection.debuglevel = 0
38
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
45
46
47 @contextmanager
48 def debug_requests():
49 """
50 Context manager to temporarily enable debug mode on requests.
51 """
52 debug_requests_on()
53 yield
54 debug_requests_off()