Skip to content

Commit 74d15d3

Browse files
committed
Fix support for Django 4.0 by using Request.build_absolute_uri()
1 parent 8994632 commit 74d15d3

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

logstash_async/formatter.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def _remove_excluded_fields(self, message, extra_fields):
191191
# ----------------------------------------------------------------------
192192
def _move_extra_record_fields_to_prefix(self, message):
193193
"""
194-
Anythng added by the "extra" keyword in the logging call will be moved into the
194+
Anything added by the "extra" keyword in the logging call will be moved into the
195195
configured "extra" prefix. This way the event in Logstash will be clean and any extras
196196
will be paired together in the configured extra prefix.
197197
If not extra prefix is configured, the message will be kept as is.
@@ -240,7 +240,7 @@ def _get_extra_fields(self, record):
240240
extra_fields['req_useragent'] = request.META.get('HTTP_USER_AGENT', '<none>')
241241
extra_fields['req_remote_address'] = request.META.get('REMOTE_ADDR', '<none>')
242242
extra_fields['req_host'] = self._try_to_get_host_from_remote(request)
243-
extra_fields['req_uri'] = request.get_raw_uri()
243+
extra_fields['req_uri'] = self._try_to_get_full_request_uri(request)
244244
extra_fields['req_user'] = str(request_user)
245245
extra_fields['req_method'] = request.META.get('REQUEST_METHOD', '')
246246
extra_fields['req_referer'] = request.META.get('HTTP_REFERER', '')
@@ -290,6 +290,14 @@ def _try_to_get_host_from_remote(self, request):
290290
else:
291291
return request.META['SERVER_NAME']
292292

293+
# ----------------------------------------------------------------------
294+
def _try_to_get_full_request_uri(self, request):
295+
try:
296+
return request.build_absolute_uri()
297+
except Exception:
298+
# build_absolute_uri() may fail with DisallowedHost errors and maybe more
299+
return None
300+
293301

294302
class FlaskLogstashFormatter(LogstashFormatter):
295303

0 commit comments

Comments
 (0)