0

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...

Dan
  • 2,209
  • 3
  • 23
  • 44

1 Answers1

0

I found an easier solution here: urllib3 debug request header

By overwriting the default debug level:

# You'll need to do this before urllib3 creates any http connection objects
import http.client
http.client.HTTPConnection.debuglevel = 5
Dan
  • 2,209
  • 3
  • 23
  • 44