Skip to content

Commit

Permalink
PB-511: Add request context to all logs
Browse files Browse the repository at this point in the history
Add middleware and logging filter to add the django request to all log records.
  • Loading branch information
benschs committed Sep 4, 2024
1 parent 0425176 commit 62f3938
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ py-multihash = "~=2.0.1"
django-prometheus = "~=2.3.1"
django-admin-autocomplete-filter = "~=0.7.1"
django-pgtrigger = "~=4.11.1"
logging-utilities = "~=4.4.1"
logging-utilities = "~=4.5.0"
django-environ = "*"

[requires]
Expand Down
8 changes: 4 additions & 4 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions app/config/logging-cfg-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ filters:
(): logging_utilities.filters.TimeAttribute
isotime: False
utc_isotime: True
add_request:
(): logging_utilities.filters.add_thread_context_filter.AddThreadContextFilter
contexts:
- logger_key: request
context_key: request
django:
(): logging_utilities.filters.django_request.JsonDjangoRequest
attr_name: request
Expand Down Expand Up @@ -133,6 +138,7 @@ handlers:
# handler, they will affect every handler
- type_filter
- isotime
- add_request
- django
# This filter only applies to the current handler (It does not modify the record in-place, but
# instead selects which logs to display)
Expand Down
3 changes: 3 additions & 0 deletions app/config/settings_prod.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@
# last, put everything else in between
MIDDLEWARE = [
'django_prometheus.middleware.PrometheusBeforeMiddleware',
# Middleware to add request to thread variables, this should be far up in the chain so request
# information can be added to as many logs as possible.
'logging_utilities.django_middlewares.add_request_context.AddToThreadContextMiddleware',
'middleware.logging.RequestResponseLoggingMiddleware',
'django.middleware.security.SecurityMiddleware',
'middleware.cors.CORSHeadersMiddleware',
Expand Down

0 comments on commit 62f3938

Please sign in to comment.