#40 Add log_debug
Merged 5 years ago by cverna. Opened 5 years ago by lenkaseg.
lenkaseg/libpagure log_debug  into  master

file modified
+18
@@ -5,6 +5,11 @@ 

  

  from .exceptions import APIError

  

+ try:

+     import http.client as http_client

+ except ImportError:

+     # Python 2

+     import httplib as http_client

  

  class NullHandler(logging.Handler):

      # Null logger to avoid spurious messages
@@ -14,6 +19,7 @@ 

  

  LOG = logging.getLogger("libpagure")

  

+ 

  # Add the null handler to top-level logger used by the library

  hand = NullHandler()

  LOG.addHandler(hand)
@@ -122,6 +128,18 @@ 

          return_value = self._call_api(request_url)

          return return_value["version"]

  

+     def log_debug(self, enabled=False):

+         """

+         Switches to debugging mode of logging.

+         """

+         if enabled:

+             http_client.HTTPConnection.debuglevel = 1

+             LOG.setLevel(logging.DEBUG)

+             requests_log = logging.getLogger("requests.packages.urllib3")

+             requests_log.setLevel(logging.DEBUG)

+             requests_log.propagate = True

+ 

+ 

      def list_users(self, pattern=None):

          """

          List all users registered on this Pagure instance.

Recently I was debugging something painfully and this feature made my life much easier:
https://stackoverflow.com/questions/10588644/how-can-i-see-the-entire-http-request-thats-being-sent-by-my-python-application

I made a function log_debug, which, when used to enable logging, shows nicely all the requests DEBUG logs.

Without:

Failed to connect to the server
Traceback (most recent call last):
File "~/cranc.py", line 134, in create_pr
branch_from=branch_from)
File "~libpagure.py", line 617, in create_pull_request
return_value = self._call_api(request_url, method="POST", data=payload)
File "~/libpagure.py", line 98, in _call_api
raise APIError(output["error"])
libpagure.exceptions.APIError: Invalid or expired token. Please visit https://pagure.io/settings#api-keys to get or renew your API token.

With :

send: b'POST /api/0/fork/lenkaseg/carrot/pull-request/new HTTP/1.1\r\nHost: pagure.io\r\nAccept-Encoding: identity\r\nAuthorization: token [blah blah]\r\nContent-Length: 49\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: b'title=veggies&branch_to=master&branch_from=readme'
reply: 'HTTP/1.1 401 UNAUTHORIZED\r\n'
header: Date: Thu, 14 Mar 2019 15:10:10 GMT
header: Server: Apache/2.4.6 (Red Hat Enterprise Linux) OpenSSL/1.0.2k-fips mod_wsgi/3.4 Python/2.7.5
header: Set-Cookie: pagure=[blah blah]; Expires=Sun, 14-Apr-2019 when it gets warm; Secure; HttpOnly; Path=/
header: X-Frame-Options: ALLOW-FROM https://pagure.io/
header: X-Xss-Protection: 1; mode=block
header: X-Content-Type-Options: nosniff
header: Referrer-Policy: same-origin
header: Content-Security-Policy: default-src 'self' https:; script-src 'self' 'unsafe-eval' 'unsafe-inline' https://apps.fedoraproject.org; style-src 'self' 'unsafe-inline' https://apps.fedoraproject.org
header: Strict-Transport-Security: max-age=3543665; includeSubDomains; preload
header: Content-Length: 154
header: Content-Type: application/json
Failed to connect to the server
Traceback (most recent call last):
File "~/cranc.py", line 134, in create_pr
branch_from=branch_from)
File ~/libpagure.py", line 617, in create_pull_request
return_value = self._call_api(request_url, method="POST", data=payload)
File "~/libpagure.py", line 98, in _call_api
raise APIError(output["error"])
libpagure.exceptions.APIError: Invalid or expired token. Please visit https://pagure.io/settings#api-keys to get or renew your API token.

Pull-Request has been merged by cverna

5 years ago
Metadata