Skip to content

Instantly share code, notes, and snippets.

@crizCraig
Created April 18, 2019 21:50
Show Gist options
  • Save crizCraig/e249f018977fbb02d38f91c23dfc7e10 to your computer and use it in GitHub Desktop.
Save crizCraig/e249f018977fbb02d38f91c23dfc7e10 to your computer and use it in GitHub Desktop.
def __log(self, verb, url, requestHeaders, input, status, responseHeaders, output):
logger = logging.getLogger(__name__)
if logger.isEnabledFor(logging.DEBUG):
if "Authorization" in requestHeaders:
if requestHeaders["Authorization"].startswith("Basic"):
requestHeaders["Authorization"] = "Basic (login and password removed)"
elif requestHeaders["Authorization"].startswith("token"):
requestHeaders["Authorization"] = "token (oauth token removed)"
elif requestHeaders["Authorization"].startswith("Bearer"):
requestHeaders["Authorization"] = "Bearer (jwt removed)"
else: # pragma no cover (Cannot happen, but could if we add an authentication method => be prepared)
requestHeaders["Authorization"] = "(unknown auth removed)" # pragma no cover (Cannot happen, but could if we add an authentication method => be prepared)
if 'BETTER_PYGITHUB_LOGGING' in os.environ:
debug_out = {
'verb': verb,
'url': '%s://%s%s' % (self.__scheme, self.__hostname, url),
'request_headers': requestHeaders,
'input': input,
'status': status,
'response_headers': responseHeaders,
'output': output
}
logger.debug(json.dumps(debug_out, indent=2))
else:
logger.debug("%s %s://%s%s %s %s ==> %i %s %s", verb, self.__scheme, self.__hostname, url, requestHeaders, input, status, responseHeaders, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment