From aa476b4318fea4acec7f0d0f712f1d4ef792955b Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Thu, 3 Feb 2022 18:38:53 +0100 Subject: [PATCH] Improved logging --- server/config/settings/base.py | 6 ++++-- server/example.env | 2 +- server/vbv_lernwelt/core/utils.py | 13 +++++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 server/vbv_lernwelt/core/utils.py diff --git a/server/config/settings/base.py b/server/config/settings/base.py index 12785be2..699c2d70 100644 --- a/server/config/settings/base.py +++ b/server/config/settings/base.py @@ -7,6 +7,8 @@ from pathlib import Path import structlog from environs import Env +from core.utils import add_app_info + SERVER_ROOT_DIR = Path(__file__).resolve(strict=True).parent.parent.parent # vbv_lernwelt/ APPS_DIR = SERVER_ROOT_DIR / "vbv_lernwelt" @@ -44,7 +46,7 @@ LOCALE_PATHS = [str(SERVER_ROOT_DIR / "locale")] # DATABASES # ------------------------------------------------------------------------------ # https://docs.djangoproject.com/en/dev/ref/settings/#databases -DATABASES = {"default": env.dj_db_url("VBV_DATABASE_URL")} +DATABASES = {"default": env.dj_db_url("VBV_DATABASE_URL", default="postgres://vbv_lernwelt@localhost:5432/vbv_lernwelt")} DATABASES["default"]["ATOMIC_REQUESTS"] = True # noqa F405 DATABASES["default"]["CONN_MAX_AGE"] = env.int("CONN_MAX_AGE", default=60) # noqa F405 # https://docs.djangoproject.com/en/stable/ref/settings/#std:setting-DEFAULT_AUTO_FIELD @@ -294,7 +296,6 @@ else: "formatters": { "json": { "()": "pythonjsonlogger.jsonlogger.JsonFormatter", - # noqa I004 "format": "%(asctime)s %(msecs)03d %(process)d %(thread)d %(levelname)s %(name)s %(filename)s %(lineno)d %(funcName)s %(message)s", # noqa I004 "datefmt": "%Y-%m-%dT%H:%M:%S", }, @@ -340,6 +341,7 @@ else: processors=[ structlog.threadlocal.merge_threadlocal, structlog.stdlib.filter_by_level, + add_app_info, structlog.stdlib.PositionalArgumentsFormatter(), structlog.processors.StackInfoRenderer(), structlog.processors.format_exc_info, diff --git a/server/example.env b/server/example.env index 1c3ab747..f5e90aee 100644 --- a/server/example.env +++ b/server/example.env @@ -1,3 +1,3 @@ export VBV_DATABASE_URL='postgres://vbv_lernwelt@localhost:5432/vbv_lernwelt' -export VBV_DJANGO_LOGGING_CONF=VBV_DJANGO_LOGGING_CONF_CONSOLE_COLOR +#export VBV_DJANGO_LOGGING_CONF=VBV_DJANGO_LOGGING_CONF_CONSOLE_COLOR export VBV_DJANGO_DEBUG=True diff --git a/server/vbv_lernwelt/core/utils.py b/server/vbv_lernwelt/core/utils.py new file mode 100644 index 00000000..af2d91a5 --- /dev/null +++ b/server/vbv_lernwelt/core/utils.py @@ -0,0 +1,13 @@ +import logging + +from django.conf import settings +from structlog.types import EventDict + + +def add_app_info( + logger: logging.Logger, method_name: str, event_dict: EventDict +) -> EventDict: + event_dict["django_app"] = "vbv_lernwelt" + event_dict["django_dev_mode"] = settings.DJANGO_DEV_MODE + + return event_dict