Log API request details to log.txt
(
self,
method: str,
url: str,
headers: Dict[str, str],
body: Optional[Any] = None,
files: Optional[Dict] = None,
proxy: Optional[str] = None
)
| 84 | return data |
| 85 | |
| 86 | def log_request( |
| 87 | self, |
| 88 | method: str, |
| 89 | url: str, |
| 90 | headers: Dict[str, str], |
| 91 | body: Optional[Any] = None, |
| 92 | files: Optional[Dict] = None, |
| 93 | proxy: Optional[str] = None |
| 94 | ): |
| 95 | """Log API request details to log.txt""" |
| 96 | |
| 97 | if not config.debug_enabled or not config.debug_log_requests: |
| 98 | return |
| 99 | |
| 100 | try: |
| 101 | self._write_separator() |
| 102 | self.logger.info(f"🔵 [REQUEST] {self._format_timestamp()}") |
| 103 | self._write_separator("-") |
| 104 | |
| 105 | # Basic info |
| 106 | self.logger.info(f"Method: {method}") |
| 107 | self.logger.info(f"URL: {url}") |
| 108 | |
| 109 | # Headers |
| 110 | self.logger.info("\n📋 Headers:") |
| 111 | masked_headers = dict(headers) |
| 112 | if "Authorization" in masked_headers or "authorization" in masked_headers: |
| 113 | auth_key = "Authorization" if "Authorization" in masked_headers else "authorization" |
| 114 | auth_value = masked_headers[auth_key] |
| 115 | if auth_value.startswith("Bearer "): |
| 116 | token = auth_value[7:] |
| 117 | masked_headers[auth_key] = f"Bearer {self._mask_token(token)}" |
| 118 | |
| 119 | # Mask Cookie header (ST token) |
| 120 | if "Cookie" in masked_headers: |
| 121 | cookie_value = masked_headers["Cookie"] |
| 122 | if "__Secure-next-auth.session-token=" in cookie_value: |
| 123 | parts = cookie_value.split("=", 1) |
| 124 | if len(parts) == 2: |
| 125 | st_token = parts[1].split(";")[0] |
| 126 | masked_headers["Cookie"] = f"__Secure-next-auth.session-token={self._mask_token(st_token)}" |
| 127 | |
| 128 | for key, value in masked_headers.items(): |
| 129 | self.logger.info(f" {key}: {value}") |
| 130 | |
| 131 | # Body |
| 132 | if body is not None: |
| 133 | self.logger.info("\n📦 Request Body:") |
| 134 | if isinstance(body, (dict, list)): |
| 135 | body_str = json.dumps(body, indent=2, ensure_ascii=False) |
| 136 | self.logger.info(body_str) |
| 137 | else: |
| 138 | self.logger.info(str(body)) |
| 139 | |
| 140 | # Files |
| 141 | if files: |
| 142 | self.logger.info("\n📎 Files:") |
| 143 | try: |
no test coverage detected