I have this class which turns on verbose logging for the httplib library which requests uses. I would like to invoke it before i run any requests code and have logging turned on. However, logging is not turned on after i call it. If i take the code out of the static method and place it in a function in the main python file and run it then it works. However i would like it packaged into a class and a static method.
import logging
import http.client
class HttpClientUtilities:
@staticmethod
def enable_logging(level=logging.DEBUG):
global http
httpclient_logger = logging.getLogger("http.client")
"""Enable HTTPConnection debug logging to the logging framework"""
def httpclient_log(*args):
httpclient_logger.log(level, " ".join(args))
# mask the print() built-in in the http.client module to use
# logging instead
http.client.print = httpclient_log
# enable debugging
http.client.HTTPConnection.debuglevel = 1
I call it like so:
from HttpClientUtilities import HttpClientUtilities
HttpClientUtilities.enable_logging()
#then here i run some methods from other classes which use the requests library
Within the enable_logging method i used global http so it should be referencing the global http object...