import os def get_settings(key): from django.conf import settings return getattr(settings, 'BASE_DIR') def get_logging_setting(): BASE_DIR = get_settings('BASE_DIR') LOG_DIR = BASE_DIR / 'log' if not os.path.exists(LOG_DIR): os.makedirs(LOG_DIR) return { '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, # }, # }, # }