From 5a745314b1c7933d106d204ae43810f25ab7f1b1 Mon Sep 17 00:00:00 2001 From: pablohashescobar Date: Fri, 15 Mar 2024 15:07:19 +0530 Subject: [PATCH] dev: logging configuration for django --- apiserver/plane/settings/common.py | 47 --------------------- apiserver/plane/settings/local.py | 43 ++++++++++++++++++-- apiserver/plane/settings/production.py | 56 +++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 52 deletions(-) diff --git a/apiserver/plane/settings/common.py b/apiserver/plane/settings/common.py index f350dd0e6..886ad4cb4 100644 --- a/apiserver/plane/settings/common.py +++ b/apiserver/plane/settings/common.py @@ -346,50 +346,3 @@ INSTANCE_KEY = os.environ.get( SKIP_ENV_VAR = os.environ.get("SKIP_ENV_VAR", "1") == "1" DATA_UPLOAD_MAX_MEMORY_SIZE = int(os.environ.get("FILE_SIZE_LIMIT", 5242880)) - -LOG_DIR = os.path.join(BASE_DIR, "logs") - -if not os.path.exists(LOG_DIR): - os.makedirs(LOG_DIR) - - -LOGGING = { - "version": 1, - "disable_existing_loggers": False, - "formatters": { - "verbose": { - "format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}", - "style": "{", - }, - "json": { - "()": "pythonjsonlogger.jsonlogger.JsonFormatter", - "fmt": "%(levelname)s %(asctime)s %(module)s %(name)s %(message)s", - }, - }, - "handlers": { - "console": { - "class": "logging.StreamHandler", - "formatter": "json", - }, - "file": { - "class": "plane.utils.logging.SizedTimedRotatingFileHandler", - "filename": ( - os.path.join(BASE_DIR, "logs", "debug.log") - if DEBUG - else os.path.join(BASE_DIR, "logs", "error.log") - ), - "when": "midnight", - "maxBytes": 1024 * 1024 * 1, - "interval": 1, # One day - "backupCount": 5, # Keep last 5 days of logs, - "formatter": "json", - }, - }, - "loggers": { - "plane": { - "level": "DEBUG" if DEBUG else "ERROR", - "handlers": ["console", "file"], - "propagate": False, - }, - }, -} diff --git a/apiserver/plane/settings/local.py b/apiserver/plane/settings/local.py index a09a55ccf..b00684eae 100644 --- a/apiserver/plane/settings/local.py +++ b/apiserver/plane/settings/local.py @@ -7,8 +7,8 @@ from .common import * # noqa DEBUG = True # Debug Toolbar settings -INSTALLED_APPS += ("debug_toolbar",) -MIDDLEWARE += ("debug_toolbar.middleware.DebugToolbarMiddleware",) +INSTALLED_APPS += ("debug_toolbar",) # noqa +MIDDLEWARE += ("debug_toolbar.middleware.DebugToolbarMiddleware",) # noqa DEBUG_TOOLBAR_PATCH_SETTINGS = False @@ -18,7 +18,7 @@ EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend" CACHES = { "default": { "BACKEND": "django_redis.cache.RedisCache", - "LOCATION": REDIS_URL, + "LOCATION": REDIS_URL, # noqa "OPTIONS": { "CLIENT_CLASS": "django_redis.client.DefaultClient", }, @@ -28,7 +28,7 @@ CACHES = { INTERNAL_IPS = ("127.0.0.1",) MEDIA_URL = "/uploads/" -MEDIA_ROOT = os.path.join(BASE_DIR, "uploads") +MEDIA_ROOT = os.path.join(BASE_DIR, "uploads") # noqa CORS_ALLOWED_ORIGINS = [ "http://localhost:3000", @@ -36,3 +36,38 @@ CORS_ALLOWED_ORIGINS = [ "http://localhost:4000", "http://127.0.0.1:4000", ] + +LOG_DIR = os.path.join(BASE_DIR, "logs") # noqa + +if not os.path.exists(LOG_DIR): + os.makedirs(LOG_DIR) + +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "verbose": { + "format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}", + "style": "{", + }, + }, + "handlers": { + "console": { + "level": "DEBUG", + "class": "logging.StreamHandler", + "formatter": "verbose", + }, + }, + "loggers": { + "django.request": { + "handlers": ["console"], + "level": "DEBUG", + "propagate": False, + }, + "plane": { + "handlers": ["console"], + "level": "DEBUG", + "propagate": False, + }, + }, +} diff --git a/apiserver/plane/settings/production.py b/apiserver/plane/settings/production.py index 5a9c3413d..aad232cc9 100644 --- a/apiserver/plane/settings/production.py +++ b/apiserver/plane/settings/production.py @@ -1,6 +1,7 @@ """Production settings""" import os + from .common import * # noqa # SECURITY WARNING: don't run with debug turned on in production! @@ -9,7 +10,7 @@ DEBUG = int(os.environ.get("DEBUG", 0)) == 1 # Honor the 'X-Forwarded-Proto' header for request.is_secure() SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") -INSTALLED_APPS += ("scout_apm.django",) +INSTALLED_APPS += ("scout_apm.django",) # noqa # Honor the 'X-Forwarded-Proto' header for request.is_secure() SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") @@ -18,3 +19,56 @@ SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") SCOUT_MONITOR = os.environ.get("SCOUT_MONITOR", False) SCOUT_KEY = os.environ.get("SCOUT_KEY", "") SCOUT_NAME = "Plane" + +LOG_DIR = os.path.join(BASE_DIR, "logs") # noqa + +if not os.path.exists(LOG_DIR): + os.makedirs(LOG_DIR) + + +LOGGING = { + "version": 1, + "disable_existing_loggers": False, + "formatters": { + "verbose": { + "format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}", + "style": "{", + }, + "json": { + "()": "pythonjsonlogger.jsonlogger.JsonFormatter", + "fmt": "%(levelname)s %(asctime)s %(module)s %(name)s %(message)s", + }, + }, + "handlers": { + "console": { + "class": "logging.StreamHandler", + "formatter": "json", + }, + "file": { + "class": "plane.utils.logging.SizedTimedRotatingFileHandler", + "filename": os.path.join(BASE_DIR, "logs", "plane.log"), # noqa + "when": "midnight", + "maxBytes": 1024 * 1024 * 1, + "interval": 1, + "backupCount": 5, + "formatter": "json", + }, + }, + "loggers": { + "django": { + "handlers": ["console", "file"], + "level": "DEBUG" if DEBUG else "ERROR", + "propagate": True, + }, + "django.request": { + "handlers": ["console", "file"], + "level": "DEBUG" if DEBUG else "ERROR", + "propagate": False, + }, + "plane": { + "level": "DEBUG" if DEBUG else "ERROR", + "handlers": ["console", "file"], + "propagate": False, + }, + }, +}