title: Python HTTP request and debug level logging to the log file
body: <p>I have a hard time to get DEBUG level logs in a log file for HTTP request like those from console like:</p>
  
  <pre><code>DEBUG:urllib3.connectionpool:Starting new HTTPS connection (1): URI:443
  DEBUG:urllib3.connectionpool:URL:443 "POST /endpoint HTTP/1.1" 200 None
  </code></pre>
  
  <p>for the code below:</p>
  
  <pre><code>import logging
  from logging.handlers import TimedRotatingFileHandler
  _logger = logging.getLogger(__name__)
  
      def setup_logging(loglevel):
          logFormatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
  
          if loglevel is not None:
              if loglevel == 10:
                    http.client.HTTPConnection.debuglevel = 1
              logformat = "%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s"
              logging.basicConfig(level=loglevel, stream=sys.stdout, format=logformat, datefmt="%Y-%m-%d %H:%M:%S")
  
          fileHandler = logging.handlers.TimedRotatingFileHandler("{0}/{1}.log".format(logPath, logFileName), when="midnight")
          fileHandler.setFormatter(logFormatter)
          _logger.setLevel(logging.DEBUG)
          _logger.addHandler(fileHandler)
  </code></pre>
  
  <p>When I will call it with <code>logging.DEBUG</code> log file will only contains whatever I will specify in the code as <code>_logger.info</code> or <code>_logger.debug</code> nothing similar like for the console log output.</p>
  
  <p>PS.
  Example code how do I call it:</p>
  
  <pre><code>def main(args):
      args = parse_args(args)
      cfg = config(args.env)
      setup_logging(logging.DEBUG, cfg)
      requests.get("https://stackoverflow.com/a/58769712/100297")
  </code></pre>