From e6eafbd6dc3627c0e4f86e88958282524b3052e8 Mon Sep 17 00:00:00 2001 From: Sayyid Hamid Mahdavi Date: Sun, 26 Apr 2026 12:35:29 +0330 Subject: [PATCH] better settings --- .gitignore | 1 + {wallet => main}/__init__.py | 0 {wallet => main}/broker_rpc.py | 2 +- {wallet => main}/celery.py | 0 {wallet => main}/exceptions.py | 0 main/other_settings/__init__.py | 0 main/other_settings/logging.py | 141 ++++++++++++++++++++++++++++++++ {wallet => main}/settings.py | 39 +-------- {wallet => main}/urls.py | 0 {wallet => main}/wsgi.py | 2 +- manage.py | 2 +- run.sh | 2 +- 12 files changed, 149 insertions(+), 40 deletions(-) rename {wallet => main}/__init__.py (100%) rename {wallet => main}/broker_rpc.py (64%) rename {wallet => main}/celery.py (100%) rename {wallet => main}/exceptions.py (100%) create mode 100644 main/other_settings/__init__.py create mode 100644 main/other_settings/logging.py rename {wallet => main}/settings.py (84%) rename {wallet => main}/urls.py (100%) rename {wallet => main}/wsgi.py (82%) diff --git a/.gitignore b/.gitignore index 7bbac41..0347d41 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ utils/delme.py *.pyc .env /venv/ +/.venv/ diff --git a/wallet/__init__.py b/main/__init__.py similarity index 100% rename from wallet/__init__.py rename to main/__init__.py diff --git a/wallet/broker_rpc.py b/main/broker_rpc.py similarity index 64% rename from wallet/broker_rpc.py rename to main/broker_rpc.py index df141d9..30950c1 100644 --- a/wallet/broker_rpc.py +++ b/main/broker_rpc.py @@ -1,6 +1,6 @@ #!/usr/bin/env python import os -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wallet.settings') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings') from utils.broker import get_rpc_broker_consumer diff --git a/wallet/celery.py b/main/celery.py similarity index 100% rename from wallet/celery.py rename to main/celery.py diff --git a/wallet/exceptions.py b/main/exceptions.py similarity index 100% rename from wallet/exceptions.py rename to main/exceptions.py diff --git a/main/other_settings/__init__.py b/main/other_settings/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/main/other_settings/logging.py b/main/other_settings/logging.py new file mode 100644 index 0000000..74038a3 --- /dev/null +++ b/main/other_settings/logging.py @@ -0,0 +1,141 @@ +import os + +def get_settings(key): + from django.conf import settings + return getattr(settings, 'BASE_DIR') + +BASE_DIR = get_settings('BASE_DIR') + +LOG_DIR = BASE_DIR / 'log' +if not os.path.exists(LOG_DIR): + os.makedirs(LOG_DIR) + + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'formatters': { + 'verbose': { + 'format': '{asctime} [{levelname}] {name} {module} {process:d} {thread:d} {message}', + 'style': '{', + }, + 'standard': { + 'format': '{asctime} [{levelname}] {name}: {message}', + 'style': '{', + }, + 'simple': { + 'format': '{levelname} {message}', + 'style': '{', + }, + }, + 'filters': { + 'require_debug_true': { + '()': 'django.utils.log.RequireDebugTrue', + }, + 'require_debug_false': { + '()': 'django.utils.log.RequireDebugFalse', + }, + }, + 'handlers': { + # هندلر برای نوشتن در فایل + 'file': { + 'level': 'INFO', + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'filename': os.path.join(LOG_DIR, 'accounts.log'), + # 'maxBytes': 1024 * 1024 * 10, # 10 MB + 'when': 'midnight', + 'interval': 5, + 'backupCount': 30, + 'formatter': 'verbose', + 'encoding': 'utf-8', + }, + # هندلر برای خطاها در فایل جداگانه + 'error_file': { + 'level': 'ERROR', + 'class': 'logging.handlers.TimedRotatingFileHandler', + 'filename': os.path.join(LOG_DIR, 'errors.log'), + # 'maxBytes': 1024 * 1024 * 10, + 'when': 'midnight', + 'interval': 5, + 'backupCount': 30, + 'formatter': 'verbose', + 'encoding': 'utf-8', + }, + # هندلر برای کنسول (فقط در حالت DEBUG) + 'console': { + 'level': 'DEBUG', + 'filters': ['require_debug_true'], + 'class': 'logging.StreamHandler', + 'formatter': 'simple', + }, + # هندلر برای کنسول همیشه فعال (حتی در production) + 'console_always': { + 'level': 'INFO', + 'class': 'logging.StreamHandler', + 'formatter': 'standard', + }, + }, + 'loggers': { + # لاگر ریشه + '': { # empty string = root logger + 'handlers': ['console', 'file', 'error_file'], + 'level': 'INFO', + 'propagate': True, + }, + # لاگر اختصاصی برای Django + 'django': { + 'handlers': ['console', 'file'], + 'level': 'INFO', + 'propagate': False, + }, + # لاگر اختصاصی برای درخواست‌ها + 'django.request': { + 'handlers': ['file', 'error_file', 'console'], + 'level': 'ERROR', + 'propagate': False, + }, + # # لاگر اختصاصی برای برنامه خودتان + # 'root': { + # 'handlers': ['console', 'file', 'error_file'], + # 'level': 'DEBUG', + # 'propagate': False, + # }, + }, +} + +# LOKI_BASE_PUBLIC_URL = config('LOKI_BASE_PUBLIC_URL', default=None, cast=str) +# { +# 'version': 1, +# 'disable_existing_loggers': False, +# 'formatters': { +# 'loki': { +# 'class': 'utils.logs.LokiFormatter', # required +# }, +# }, +# +# 'handlers': { +# 'loki': { +# 'level': 'DEBUG', # Log level. Required +# 'class': 'utils.logs.LokiHandler', # Required +# 'formatter': 'loki', # Loki formatter. Required +# 'timeout': 2, # Post request timeout, default is 0.5. Optional +# 'url': f'{LOKI_BASE_PUBLIC_URL}/loki/api/v1/push', # Loki url. Defaults to localhost. Optional. +# # 'auth': ("user", "password"), # Basic auth to authenticate with loki. Default is None (i.e. no auth). Optional +# 'tags': {"app": "accounts"}, # Tags / Labels to attach to the log. Optional, but strongly encoraged to use. +# 'mode': 'thread', +# # Push mode. Can be 'sync' or 'thread'. Sync is blocking, thread is non-blocking. Defaults to sync. Optional. +# }, +# 'console': { +# 'level': 'DEBUG', +# 'class': 'logging.StreamHandler', +# # 'formatter': 'verbose', +# }, +# }, +# 'loggers': { +# '': { +# 'handlers': ['console', 'loki'], +# 'level': 'INFO', +# 'propagate': True, +# }, +# }, +# } \ No newline at end of file diff --git a/wallet/settings.py b/main/settings.py similarity index 84% rename from wallet/settings.py rename to main/settings.py index be7c6d6..59f47a3 100644 --- a/wallet/settings.py +++ b/main/settings.py @@ -125,7 +125,7 @@ SPECTACULAR_SETTINGS = { # https://spec.openapis.org/oas/v3.0.3#oauth-flows-object } -ROOT_URLCONF = 'wallet.urls' +ROOT_URLCONF = 'main.urls' TEMPLATES = [ { @@ -148,7 +148,7 @@ AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', ) -WSGI_APPLICATION = 'wallet.wsgi.application' +WSGI_APPLICATION = 'main.wsgi.application' # Database # https://docs.djangoproject.com/en/5.0/ref/settings/#databases @@ -256,39 +256,6 @@ CACHES = { LOKI_BASE_PUBLIC_URL = config('LOKI_BASE_PUBLIC_URL', default=None, cast=str) -LOGGING = { - 'version': 1, - 'disable_existing_loggers': False, - 'formatters': { - 'loki': { - 'class': 'utils.logs.LokiFormatter', # required - }, - }, - - 'handlers': { - 'loki': { - 'level': 'DEBUG', # Log level. Required - 'class': 'utils.logs.LokiHandler', # Required - 'formatter': 'loki', # Loki formatter. Required - 'timeout': 2, # Post request timeout, default is 0.5. Optional - 'url': f'{LOKI_BASE_PUBLIC_URL}/loki/api/v1/push', # Loki url. Defaults to localhost. Optional. - # 'auth': ("user", "password"), # Basic auth to authenticate with loki. Default is None (i.e. no auth). Optional - 'tags': {"app": "wallet"}, # Tags / Labels to attach to the log. Optional, but strongly encoraged to use. - 'mode': 'thread', # Push mode. Can be 'sync' or 'thread'. Sync is blocking, thread is non-blocking. Defaults to sync. Optional. - }, - 'console': { - 'level': 'DEBUG', - 'class': 'logging.StreamHandler', - # 'formatter': 'verbose', - }, - }, - 'loggers': { - '': { - 'handlers': ['console', 'loki'], - 'level': 'INFO', - 'propagate': True, - }, - }, -} +from main.other_settings.logging import LOGGING ALLOWED_NEGATIVE_BALANCE_APPLICATIONS = config('ALLOWED_NEGATIVE_BALANCE_APPLICATIONS', cast=lambda v: [s.strip() for s in v.split(',')]) diff --git a/wallet/urls.py b/main/urls.py similarity index 100% rename from wallet/urls.py rename to main/urls.py diff --git a/wallet/wsgi.py b/main/wsgi.py similarity index 82% rename from wallet/wsgi.py rename to main/wsgi.py index 68001d5..62737e2 100644 --- a/wallet/wsgi.py +++ b/main/wsgi.py @@ -11,6 +11,6 @@ import os from django.core.wsgi import get_wsgi_application -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wallet.settings') +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings') application = get_wsgi_application() diff --git a/manage.py b/manage.py index ffd5d2d..063eacc 100644 --- a/manage.py +++ b/manage.py @@ -5,7 +5,7 @@ import sys def main(): - os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'wallet.settings') + os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'main.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: diff --git a/run.sh b/run.sh index 0a07a3f..2ebdb8a 100755 --- a/run.sh +++ b/run.sh @@ -5,4 +5,4 @@ while ! nc -z $DB_HOST 5432 ; do done #python3 manage.py collectstatic --noinput python3 manage.py migrate -gunicorn wallet.wsgi:application --bind 0.0.0.0:8000 -w 4 \ No newline at end of file +gunicorn main.wsgi:application --bind 0.0.0.0:8000 -w 4 \ No newline at end of file