|
17 | 17 | caching system to set a key and then triggering the actual event from middleware
|
18 | 18 | """
|
19 | 19 |
|
| 20 | +import json |
20 | 21 | import logging
|
| 22 | +import uuid |
21 | 23 | from http import HTTPStatus
|
22 | 24 | from threading import local
|
23 | 25 |
|
@@ -53,8 +55,49 @@ def __call__(self, request):
|
53 | 55 | if request.user.is_authenticated:
|
54 | 56 | _user.value = request.user
|
55 | 57 |
|
| 58 | + # Log all requests to our v2 API endpoints |
| 59 | + if settings.ENABLE_API_LOGGING and request.path.startswith("/rest/v2/"): |
| 60 | + try: |
| 61 | + # Generate a trace ID for each request |
| 62 | + trace_id = str(uuid.uuid4()) |
| 63 | + request.trace_id = trace_id |
| 64 | + # Log the IP address of the request |
| 65 | + ip_address = request.META.get("REMOTE_ADDR") |
| 66 | + logger.info(f"[Trace ID: {trace_id}] IP Address: {ip_address}") |
| 67 | + # Log the user if authenticated |
| 68 | + if request.user.is_authenticated: |
| 69 | + logger.info(f"[Trace ID: {trace_id}] User: {request.user.username}") |
| 70 | + # Log the request details |
| 71 | + logger.info( |
| 72 | + f"[Trace ID: {request.trace_id}] Request: {request.method} {request.get_full_path()}" |
| 73 | + ) |
| 74 | + if request.body: |
| 75 | + logger.info( |
| 76 | + f"[Trace ID: {request.trace_id}] Request body: {json.loads(request.body)}" |
| 77 | + ) |
| 78 | + except Exception as e: |
| 79 | + logger.exception( |
| 80 | + f"[Trace ID: {request.trace_id}] An error occurred when parsing request: {str(e)}" |
| 81 | + ) |
| 82 | + |
56 | 83 | response = self.get_response(request)
|
57 | 84 |
|
| 85 | + # Log all responses to our v2 API endpoints |
| 86 | + if settings.ENABLE_API_LOGGING and request.path.startswith("/rest/v2/"): |
| 87 | + try: |
| 88 | + # Log the response details |
| 89 | + logger.info( |
| 90 | + f"[Trace ID: {request.trace_id}] Response status: {response.status_code}" |
| 91 | + ) |
| 92 | + if hasattr(response, "content") and response.content: |
| 93 | + logger.info( |
| 94 | + f"[Trace ID: {request.trace_id}] Response body: {json.loads(response.content)}" |
| 95 | + ) |
| 96 | + except Exception as e: |
| 97 | + logger.exception( |
| 98 | + f"[Trace ID: {request.trace_id}] An error occurred when parsing response: {str(e)}" |
| 99 | + ) |
| 100 | + |
58 | 101 | triggers = {
|
59 | 102 | "Config": trigger_write_config.send,
|
60 | 103 | "Rules": trigger_write_rules.send,
|
|
0 commit comments