Skip to content

Commit

Permalink
Improve debug logging to make it faster to understand
Browse files Browse the repository at this point in the history
This change clearly labels each section that's printed to make it easier
to identify the interesting parts.

This also adds SUBMAN_DEBUG_PRINT_TRACEBACKS flag to print the traceback
directly to stdout.
  • Loading branch information
m-horky committed Dec 11, 2023
1 parent 5ffa8be commit 70110fd
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/rhsm/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,12 +874,14 @@ def _print_debug_info_about_request(
and self.__conn is not None
and self.__conn.sock is not None
):
msg += green_col + "TCP socket:\n" + end_col
msg += blue_col + f"{self.__conn.sock}\n" + end_col

# When proxy server is used, then print some additional information about proxy connection
if self.proxy_hostname and self.proxy_port:
msg += green_col + "Proxy:\n" + end_col
# Note: using only https:// is not a mistake. We use only https for proxy connection.
msg += blue_col + "Using proxy: " + magenta_col + "https://"
msg += magenta_col + "https://"
# Print username and eventually password
if self.proxy_user:
if self.proxy_user and self.proxy_password:
Expand All @@ -897,24 +899,31 @@ def _print_debug_info_about_request(
msg += blue_col + f" {tunel_headers}" + end_col
msg += "\n"

if self.insecure is True:
msg += blue_col + f"Making insecure ({auth}) request:" + end_col
else:
msg += blue_col + f"Making ({auth}) request:" + end_col
msg += green_col + "Request:\n" + end_col
msg += (
red_col
+ " https://"
+ "https://"
+ f"{normalized_host(self.host)}:{safe_int(self.ssl_port)}{handler} {request_type}"
+ end_col
)
if self.insecure:
msg += blue_col + f" using insecure {auth}\n" + end_col
else:
msg += blue_col + f" using {auth}\n" + end_col

if os.environ.get("SUBMAN_DEBUG_PRINT_REQUEST_HEADER", ""):
msg += blue_col + " %s" % final_headers + end_col
msg += green_col + "Headers:\n" + end_col
msg += blue_col + "%s" % final_headers + end_col
if os.environ.get("SUBMAN_DEBUG_PRINT_REQUEST_BODY", "") and body is not None:
msg += yellow_col + " %s" % body + end_col
msg += green_col + "Body:\n" + end_col
msg += yellow_col + "%s" % body + end_col
print()
print(msg)
print()

if os.environ.get("SUBMAN_DEBUG_PRINT_TRACEBACKS", ""):
print(green_col + "Traceback:" + end_col)
traceback.print_stack(file=sys.stdout)

if os.environ.get("SUBMAN_DEBUG_SAVE_TRACEBACKS", ""):
debug_dir = Path("/tmp/rhsm/")
debug_dir.mkdir(exist_ok=True)
Expand All @@ -933,8 +942,10 @@ def _print_debug_info_about_request(
with debug_log.open("w", encoding="utf-8") as handle:
traceback.print_stack(file=handle)

print(green_col + f"Traceback saved to {str(debug_log)}." + end_col)
print()
print(green_col + "Traceback file:" + end_col)
print(f"{str(debug_log)}" + end_col)

print()

@staticmethod
def _print_debug_info_about_response(result: dict) -> None:
Expand All @@ -947,9 +958,14 @@ def _print_debug_info_about_response(result: dict) -> None:

if os.environ.get("SUBMAN_DEBUG_PRINT_RESPONSE", ""):
gray_col = "\033[90m"
green_col = "\033[92m"
end_col = "\033[0m"
print(gray_col + "%s %s" % (result["status"], result["headers"]))
print(result["content"] + end_col)

print(green_col + "Response:" + end_col)
print(gray_col + "%s %s" % (result["status"], result["headers"]) + end_col)
if result["content"]:
print(green_col + "Response content:" + end_col)
print(gray_col + result["content"] + end_col)
print()

def _set_accept_language_in_header(self) -> None:
Expand Down

0 comments on commit 70110fd

Please sign in to comment.