Skip to content

Commit f66447e

Browse files
committed
feat: change log format to json
- Add django-logger-extra - Configure in Django - Configure in uWSGI - Use common uWSGI components Refs: NS-181
1 parent bf316d2 commit f66447e

5 files changed

Lines changed: 46 additions & 4 deletions

File tree

.prod/uwsgi.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[uwsgi]
22
strict = true
3+
include = uwsgi-base.ini
34

45
http-socket = :8000
56
chdir = /app

Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
FROM registry.access.redhat.com/ubi9/python-312 AS appbase
33
# ==============================
44

5+
# Branch or tag used to pull python-uwsgi-common.
6+
ARG UWSGI_COMMON_REF=main
7+
58
USER root
69
WORKDIR /app
710

@@ -23,6 +26,15 @@ RUN dnf update -y && dnf install -y nc \
2326
&& mkdir /entrypoint \
2427
&& dnf clean all
2528

29+
# Build and copy specific python-uwsgi-common files.
30+
ADD https://github.com/City-of-Helsinki/python-uwsgi-common/archive/${UWSGI_COMMON_REF}.tar.gz /usr/src/
31+
RUN mkdir -p /usr/src/python-uwsgi-common && \
32+
tar --strip-components=1 -xzf /usr/src/${UWSGI_COMMON_REF}.tar.gz -C /usr/src/python-uwsgi-common && \
33+
cp /usr/src/python-uwsgi-common/uwsgi-base.ini /app && \
34+
uwsgi --build-plugin /usr/src/python-uwsgi-common && \
35+
rm -rf /usr/src/${UWSGI_COMMON_REF}.tar.gz && \
36+
rm -rf /usr/src/python-uwsgi-common
37+
2638
COPY --chown=default:root docker-entrypoint.sh /entrypoint/docker-entrypoint.sh
2739
CMD ["/usr/bin/bash", "/entrypoint/docker-entrypoint.sh"]
2840

notification_service/settings.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,14 @@ def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
151151
"common",
152152
"custom_health_checks",
153153
"audit_log",
154+
"logger_extra",
154155
]
155156

156157
MIDDLEWARE = [
157158
"django.middleware.security.SecurityMiddleware",
158159
"django.contrib.sessions.middleware.SessionMiddleware",
159160
"corsheaders.middleware.CorsMiddleware",
161+
"logger_extra.middleware.XRequestIdMiddleware",
160162
"csp.middleware.CSPMiddleware",
161163
"django.middleware.locale.LocaleMiddleware",
162164
"django.middleware.common.CommonMiddleware",
@@ -256,8 +258,29 @@ def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
256258
LOGGING = {
257259
"version": 1,
258260
"disable_existing_loggers": False,
259-
"handlers": {"console": {"class": "logging.StreamHandler"}},
260-
"loggers": {"django": {"handlers": ["console"], "level": "ERROR"}},
261+
"filters": {
262+
"context": {
263+
"()": "logger_extra.filter.LoggerContextFilter",
264+
}
265+
},
266+
"formatters": {
267+
"json": {
268+
"()": "logger_extra.formatter.JSONFormatter",
269+
}
270+
},
271+
"handlers": {
272+
"console": {
273+
"class": "logging.StreamHandler",
274+
"formatter": "json",
275+
"filters": ["context"],
276+
},
277+
},
278+
"loggers": {
279+
"django": {
280+
"handlers": ["console"],
281+
"level": "ERROR",
282+
},
283+
},
261284
}
262285

263286
# local_settings.py can be used to override environment-specific settings

requirements.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
django~=5.2
1+
Django~=5.2
2+
django-axes
23
django-cors-headers
34
django-csp
45
django-environ
56
django-helusers
67
django-health-check
7-
django-axes
8+
django-logger-extra
89
djangorestframework
910
factory-boy
1011
psycopg[c]

requirements.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ django==5.2.8 \
298298
# django-csp
299299
# django-health-check
300300
# django-helusers
301+
# django-logger-extra
301302
# djangorestframework
302303
# sentry-sdk
303304
# social-auth-app-django
@@ -325,6 +326,10 @@ django-helusers==0.14.0 \
325326
--hash=sha256:424f97e7aa6792ae255de56344f765795444849ea82e9d4e01666494c28e960c \
326327
--hash=sha256:79b922d51278a9e9793a00279782e5455b72e9e9d0f261dd17be2f0dbcbaa1f9
327328
# via -r requirements.in
329+
django-logger-extra==1.1.0 \
330+
--hash=sha256:bc9450ead1e87aad1866210657d1a9f8efe9e21bfea9b53618d9fd0c829f1179 \
331+
--hash=sha256:f44ab408c812474163a21b13a7acba48407ba52b128260370e8f4ccb2ecab89b
332+
# via -r requirements.in
328333
djangorestframework==3.16.1 \
329334
--hash=sha256:166809528b1aced0a17dc66c24492af18049f2c9420dbd0be29422029cfc3ff7 \
330335
--hash=sha256:33a59f47fb9c85ede792cbf88bde71893bcda0667bc573f784649521f1102cec

0 commit comments

Comments
 (0)