function logged_request_v2
A wrapper function that logs HTTP requests and responses for debugging and analysis purposes, capturing request details, response status, and body content.
/tf/active/vicechatdev/e-ink-llm/cloudtest/test_real_app_upload.py
21 - 65
moderate
Purpose
This function intercepts HTTP requests to log comprehensive metadata including timestamps, headers, request/response bodies, and status codes. It provides special handling for binary vs text data, JSON payloads, and includes console output for requests to 'tectonic.remarkable.com'. The logged data is stored in a 'raw_logs' list for later analysis. This is typically used as a monkey-patch replacement for the standard requests library's request method to enable request/response inspection.
Source Code
def logged_request(self, method, url, *args, **kwargs):
"""Log raw HTTP requests for analysis"""
headers = kwargs.get('headers', {})
data = kwargs.get('data', b'')
json_data = kwargs.get('json')
log_entry = {
'timestamp': time.time(),
'method': method.upper(),
'url': url,
'headers': dict(headers),
'body_size': len(data) if data else 0,
'has_json': json_data is not None
}
if json_data:
log_entry['json_body'] = json_data
if data:
try:
text_body = data.decode('utf-8')
log_entry['body_text'] = text_body
log_entry['body_type'] = 'text'
except UnicodeDecodeError:
log_entry['body_text'] = f"<binary data: {len(data)} bytes>"
log_entry['body_type'] = 'binary'
response = original_request(self, method, url, *args, **kwargs)
log_entry['response_status'] = response.status_code
log_entry['response_size'] = len(response.text) if response.text else 0
log_entry['response_text'] = response.text
raw_logs.append(log_entry)
if 'tectonic.remarkable.com' in url:
print(f"\nš API REQUEST #{len(raw_logs)}:")
print(f" {method.upper()} {url}")
print(f" Response: {response.status_code}")
if response.text and len(response.text) < 500:
print(f" Response: {response.text}")
elif response.text:
print(f" Response: {len(response.text)} chars")
return response
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
self |
- | - | positional_or_keyword |
method |
- | - | positional_or_keyword |
url |
- | - | positional_or_keyword |
*args |
- | - | var_positional |
**kwargs |
- | - | var_keyword |
Parameter Details
self: The instance object (typically a requests.Session or similar HTTP client object) that this method is bound to. Required for calling the original request method.
method: HTTP method as a string (e.g., 'GET', 'POST', 'PUT', 'DELETE'). Will be converted to uppercase for logging.
url: The target URL for the HTTP request as a string. Used to determine if special logging output should be displayed (checks for 'tectonic.remarkable.com').
*args: Variable positional arguments that are passed through to the underlying original_request function without modification.
**kwargs: Variable keyword arguments passed to the original request. Expected keys include 'headers' (dict), 'data' (bytes), and 'json' (dict/list). All kwargs are forwarded to the original request function.
Return Value
Returns a Response object from the underlying HTTP library (typically requests.Response), containing the server's response including status code, headers, and body content. The response is returned unmodified after logging is complete.
Dependencies
timerequests
Required Imports
import time
import requests
Usage Example
import time
import requests
# Setup: Store original request method and initialize log storage
original_request = requests.Session.request
raw_logs = []
# Monkey-patch the Session.request method
requests.Session.request = logged_request
# Now all requests through Session will be logged
session = requests.Session()
response = session.get('https://api.example.com/data', headers={'Authorization': 'Bearer token'})
# Access logged data
print(f"Total requests logged: {len(raw_logs)}")
for log in raw_logs:
print(f"{log['method']} {log['url']} - Status: {log['response_status']}")
Best Practices
- Ensure 'original_request' is properly saved before monkey-patching to avoid infinite recursion
- Initialize 'raw_logs' as a list in the appropriate scope (module-level or class-level) before using this function
- Be cautious about logging sensitive data in headers or request bodies - consider sanitizing authentication tokens
- Monitor memory usage when logging large responses, as all response text is stored in raw_logs
- This function modifies global state (raw_logs list) - consider thread-safety if used in multi-threaded environments
- The console output for 'tectonic.remarkable.com' URLs may expose sensitive information - use only in development/debugging
- Consider implementing log rotation or size limits for raw_logs to prevent unbounded memory growth in long-running applications
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function logged_request_v1 89.1% similar
-
function logged_request 84.1% similar
-
class RawRequestLogger 72.6% similar
-
function log_debug 50.0% similar
-
function main_v40 49.9% similar