Skip to content

Commit

Permalink
Merge pull request #648 from vintasoftware/add-django-guid
Browse files Browse the repository at this point in the history
Add django-guid
  • Loading branch information
fjsj authored May 6, 2024
2 parents 8f205f5 + bdf6c41 commit c0b58fe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Also, includes a Render.com `render.yaml` and a working Django `production.py` s
- `django-webpack-loader` for rendering the bundled frontend assets
- `django-js-reverse` for easy handling of Django URLs on JS
- `django-upgrade` for automatically upgrading Django code to the target version on pre-commit
- `django-guid` for adding a unique correlation ID to log messages from Django requests
- `psycopg2` for using PostgreSQL database
- `sentry-sdk` for error monitoring
- `python-decouple` for reading environment variables on settings files
Expand Down
4 changes: 3 additions & 1 deletion backend/project_name/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def base_dir_join(*args):
"import_export",
"rest_framework",
"defender",
"django_guid",
"common",
"users",
]
Expand All @@ -54,6 +55,7 @@ def base_dir_join(*args):
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"csp.middleware.CSPMiddleware",
"defender.middleware.FailedLoginMiddleware",
"django_guid.middleware.guid_middleware",
]

ROOT_URLCONF = "{{project_name}}.urls"
Expand Down Expand Up @@ -205,7 +207,7 @@ def base_dir_join(*args):
] + [f"*{host}" if host.startswith(".") else host for host in ALLOWED_HOSTS]
CSP_CONNECT_SRC = [
"'self'",
"https://sentry.io",
"*.sentry.io",
] + [f"*{host}" if host.startswith(".") else host for host in ALLOWED_HOSTS]
CSP_STYLE_SRC = [
"'self'",
Expand Down
13 changes: 12 additions & 1 deletion backend/project_name/settings/local_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,30 @@
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"filters": {
"correlation_id": {"()": "django_guid.log_filters.CorrelationId"},
},
"formatters": {
"standard": {"format": "%(levelname)-8s [%(asctime)s] %(name)s: %(message)s"},
"standard": {
"format": "%(levelname)-8s [%(asctime)s] [%(correlation_id)s] %(name)s: %(message)s"
},
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "standard",
"filters": ["correlation_id"],
},
},
"loggers": {
"": {"handlers": ["console"], "level": "INFO"},
"celery": {"handlers": ["console"], "level": "INFO"},
"django_guid": {
"handlers": ["console"],
"level": "WARNING",
"propagate": False,
},
},
}

Expand Down
18 changes: 16 additions & 2 deletions backend/project_name/settings/production.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import sentry_sdk
from decouple import Csv, config
from django_guid.integrations import SentryIntegration as DjangoGUIDSentryIntegration
from sentry_sdk.integrations.django import DjangoIntegration

from .base import *
Expand Down Expand Up @@ -62,6 +63,13 @@
},
}

# Django GUID
DJANGO_GUID = {
"INTEGRATIONS": [
DjangoGUIDSentryIntegration(),
],
}

# django-log-request-id
MIDDLEWARE.insert( # insert RequestIDMiddleware on the top
0, "log_request_id.middleware.RequestIDMiddleware"
Expand All @@ -76,10 +84,11 @@
"filters": {
"require_debug_false": {"()": "django.utils.log.RequireDebugFalse"},
"request_id": {"()": "log_request_id.filters.RequestIDFilter"},
"correlation_id": {"()": "django_guid.log_filters.CorrelationId"},
},
"formatters": {
"standard": {
"format": "%(levelname)-8s [%(asctime)s] [%(request_id)s] %(name)s: %(message)s"
"format": "%(levelname)-8s [%(asctime)s] [%(request_id)s] [%(correlation_id)s] %(name)s: %(message)s"
},
},
"handlers": {
Expand All @@ -94,7 +103,7 @@
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"filters": ["request_id"],
"filters": ["request_id", "correlation_id"],
"formatter": "standard",
},
},
Expand All @@ -114,6 +123,11 @@
"level": "DEBUG",
"propagate": False,
},
"django_guid": {
"handlers": ["console"],
"level": "WARNING",
"propagate": False,
},
},
}

Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ setuptools = "^69.0.2"
django-permissions-policy = "^4.18.0"
django-csp = "^3.7"
django-defender = "^0.9.7"
django-guid = "^3.4.0"

[tool.poetry.group.dev.dependencies]
coverage = "^7.2.7"
Expand Down

0 comments on commit c0b58fe

Please sign in to comment.