Skip to content

Commit

Permalink
log response in separate middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
copelco committed Nov 2, 2024
1 parent c99b17b commit 480e028
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions nc/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ def __init__(self, get_response):
def __call__(self, request: HttpRequest):
headers = {"_type": "request", "_path": request.get_full_path(), "_now": now().isoformat()}
headers.update(request.headers)
for header in ("wsgi.input", "wsgi.errors", "wsgi.file_wrapper"):
headers.pop(header, None)
print(json.dumps(headers))
return self.get_response(request)


class ResponseLoggingMiddleware:
"""Log response headers"""

def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request: HttpRequest):
response: HttpResponse = self.get_response(request)
headers = {"_type": "response", "_path": request.get_full_path(), "_now": now().isoformat()}
headers.update(response.headers)
Expand Down
1 change: 1 addition & 0 deletions traffic_stops/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def __init__(self, tz_name=None):
]

MIDDLEWARE = [
"nc.middleware.ResponseLoggingMiddleware",
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
Expand Down
2 changes: 1 addition & 1 deletion traffic_stops/settings/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
NC_AUTO_IMPORT_MONITORS = ("[email protected]",)

ALLOWED_HOSTS = os.getenv("ALLOWED_HOSTS", "")
ALLOWED_HOSTS = ALLOWED_HOSTS.split(",") if ALLOWED_HOSTS else []
ALLOWED_HOSTS = ALLOWED_HOSTS.split(",") if ALLOWED_HOSTS else ["*"]

# Special test settings
if "test" in sys.argv:
Expand Down

0 comments on commit 480e028

Please sign in to comment.