Skip to content

Commit 0310667

Browse files
committed
feat: change log format to json
- Add django-logger-extra - Configure in Django - Configure in uWSGI - Use common uWSGI components Refs: PT-1967
1 parent 4d49f66 commit 0310667

5 files changed

Lines changed: 62 additions & 7 deletions

File tree

.prod/uwsgi.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[uwsgi]
22
# https://uwsgi-docs.readthedocs.io/en/latest/Options.html
33
strict = true # Fail if any option is unknown
4+
include = uwsgi-base.ini
45

56
http-socket = :8000
67
http-timeout = 60

Dockerfile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# ==============================
22
FROM registry.access.redhat.com/ubi9/python-312 AS appbase
33
# ==============================
4+
5+
# Branch or tag used to pull python-uwsgi-common.
6+
ARG UWSGI_COMMON_REF=main
7+
48
USER root
59
WORKDIR /app
610

@@ -21,6 +25,15 @@ RUN dnf update -y \
2125
&& uwsgi --build-plugin https://github.com/City-of-Helsinki/uwsgi-sentry \
2226
&& dnf clean all
2327

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

palvelutarjotin/settings.py

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,14 @@ def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
251251
"reports",
252252
"notification_importers",
253253
"palvelutarjotin",
254+
"logger_extra",
254255
]
255256

256257
MIDDLEWARE = [
257258
"django.middleware.security.SecurityMiddleware",
258259
"django.contrib.sessions.middleware.SessionMiddleware",
259260
"corsheaders.middleware.CorsMiddleware",
261+
"logger_extra.middleware.XRequestIdMiddleware",
260262
"csp.middleware.CSPMiddleware",
261263
"django.middleware.locale.LocaleMiddleware",
262264
"django.middleware.common.CommonMiddleware",
@@ -440,16 +442,49 @@ def sentry_traces_sampler(sampling_context: SamplingContext) -> float:
440442
LOGGING = {
441443
"version": 1,
442444
"disable_existing_loggers": False,
443-
"handlers": {"console": {"class": "logging.StreamHandler"}},
444-
"loggers": {"django": {"handlers": ["console"], "level": "ERROR"}},
445+
"filters": {
446+
"context": {
447+
"()": "logger_extra.filter.LoggerContextFilter",
448+
}
449+
},
450+
"formatters": {
451+
"json": {
452+
"()": "logger_extra.formatter.JSONFormatter",
453+
}
454+
},
455+
"handlers": {
456+
"console": {
457+
"class": "logging.StreamHandler",
458+
"formatter": "json",
459+
"filters": ["context"],
460+
},
461+
},
462+
"loggers": {
463+
"django": {
464+
"handlers": ["console"],
465+
"level": "ERROR",
466+
},
467+
},
445468
}
446469

447470
if DEBUG is True:
448471
LOGGING["loggers"] = {
449-
"django": {"handlers": ["console"], "level": "WARNING"},
450-
"occurrences": {"handlers": ["console"], "level": "DEBUG"},
451-
"organisations": {"handlers": ["console"], "level": "DEBUG"},
452-
"palvelutarjotin": {"handlers": ["console"], "level": "DEBUG"},
472+
"django": {
473+
"handlers": ["console"],
474+
"level": "WARNING",
475+
},
476+
"occurrences": {
477+
"handlers": ["console"],
478+
"level": "DEBUG",
479+
},
480+
"organisations": {
481+
"handlers": ["console"],
482+
"level": "DEBUG",
483+
},
484+
"palvelutarjotin": {
485+
"handlers": ["console"],
486+
"level": "DEBUG",
487+
},
453488
}
454489

455490
CAPTCHA_ENABLED = env.bool("CAPTCHA_ENABLED")

requirements.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ django-graphql-jwt
1212
django-health-check
1313
django-helusers
1414
django-ilmoitin
15+
django-logger-extra
1516
django-parler
1617
django-utils-six
1718
drf-spectacular

requirements.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This file is autogenerated by pip-compile with Python 3.12
33
# by the following command:
44
#
5-
# pip-compile --allow-unsafe --cert=None --client-cert=None --generate-hashes --index-url=None --pip-args=None --strip-extras requirements.in
5+
# pip-compile --allow-unsafe --generate-hashes --strip-extras requirements.in
66
#
77
asgiref==3.10.0 \
88
--hash=sha256:aef8a81283a34d0ab31630c9b7dfe70c812c95eba78171367ca8745e88124734 \
@@ -292,6 +292,7 @@ django==5.2.8 \
292292
# django-health-check
293293
# django-helusers
294294
# django-ilmoitin
295+
# django-logger-extra
295296
# django-mailer
296297
# django-parler
297298
# djangorestframework
@@ -355,6 +356,10 @@ django-ilmoitin==0.7.1 \
355356
--hash=sha256:02931abd50d857ac30195e8ece533b19a0c9e5d6912d86f04044030917f5fa54 \
356357
--hash=sha256:8c0ced97271a1233027da6267cab67490ade4ef78026d24e6365dbf5c3f413ef
357358
# via -r requirements.in
359+
django-logger-extra==1.1.0 \
360+
--hash=sha256:bc9450ead1e87aad1866210657d1a9f8efe9e21bfea9b53618d9fd0c829f1179 \
361+
--hash=sha256:f44ab408c812474163a21b13a7acba48407ba52b128260370e8f4ccb2ecab89b
362+
# via -r requirements.in
358363
django-mailer==2.3.2 \
359364
--hash=sha256:6ea0cbec09f57eb91376780a5f20ab7a834b942435a575e92e8e021f7838d78f \
360365
--hash=sha256:c7ef5ad63d5484246908bd1ce583c22837c66c1a7b593c9b816f4ead2421c995

0 commit comments

Comments
 (0)