From fd0f0b56bda08f0fecd565a15712940b2da92e5f Mon Sep 17 00:00:00 2001 From: PCoder Date: Sun, 10 Feb 2019 23:32:59 +0100 Subject: [PATCH] Load configs from .env and add basic logging config --- ungleichotpserver/settings.py | 45 +++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/ungleichotpserver/settings.py b/ungleichotpserver/settings.py index 6828a5a..2be3f0f 100644 --- a/ungleichotpserver/settings.py +++ b/ungleichotpserver/settings.py @@ -10,6 +10,8 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.1/ref/settings/ """ +from decouple import config, Csv + import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) @@ -20,7 +22,7 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = 'h^*!&u7yaac_6t02kk4de%$aagp6_j#+_wnw3@rqu6os0tlv#r' +SECRET_KEY = config('SECRET_KEY') # SECURITY WARNING: don't run with debug turned on in production! @@ -129,7 +131,7 @@ DEBUG_DATABASES = { } } -DEBUG = False +DEBUG = config('DEBUG', False, cast=bool) ALLOWED_HOSTS = [ ".ungleich.ch" ] @@ -146,6 +148,45 @@ BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_ROOT = os.path.join(BASE_DIR, "static") STATIC_URL = '/static/' +LOGGING = { + 'disable_existing_loggers': False, + 'version': 1, + 'formatters': { + 'standard': { + 'format': '%(asctime)s %(levelname)s %(name)s: %(message)s' + } + }, + 'handlers': { + 'default': { + 'level': 'DEBUG', + 'class': 'logging.handlers.RotatingFileHandler', + 'filename': 'logs/debug.log', + 'maxBytes': 1024*1024*5, + 'backupCount': 10, + 'formatter': 'standard', + }, + 'console': { + 'class': 'logging.StreamHandler', + }, + }, +} + +if config('ENABLE_DEBUG_LOG', cast=bool, default=False): + loggers_dict = {} + modules_to_log_list = config( + 'MODULES_TO_LOG', default='django', cast=Csv() + ) + for custom_module in modules_to_log_list: + logger_item = { + custom_module: { + 'handlers': ['default'], + 'level': 'DEBUG', + 'propagate': True + } + } + loggers_dict.update(logger_item) + + LOGGING['loggers'] = loggers_dict if "DEBUG" in os.environ: