""" WSGI config for rad project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.11/howto/deployment/wsgi/ """ import os from django.core.wsgi import get_wsgi_application from loguru import logger import sys from pathlib import Path import logging # Configure Loguru sinks early so app imports see consistent logging. # Log to stdout (so `docker logs` shows messages) and to a file at # `/var/log/rad/app.log` for persistent collection / host tailing. try: # ensure directory exists log_dir = Path("/var/log/rad") log_dir.mkdir(parents=True, exist_ok=True) # Determine stdout level: allow overriding with LOGURU_STDOUT_LEVEL env var, # otherwise expose DEBUG when DEBUG env var is truthy, fall back to INFO. raw_debug = os.environ.get("DEBUG", "0") env_level = os.environ.get("LOGURU_STDOUT_LEVEL") if env_level: stdout_level = env_level else: stdout_level = "DEBUG" if raw_debug.lower() in ("1", "true", "yes") else "INFO" # Add stdout sink (if not already added by other code) logger.remove() # remove default handlers to avoid duplicate lines logger.add(sys.stdout, level=stdout_level, enqueue=True, backtrace=False, diagnose=False) # Add rotating file sink for persistence in container. Use JSON lines # (serialize=True) to make logs easy to query in Loki/Grafana. logger.add( str(log_dir / "app.log"), rotation="10 MB", retention="10 days", level="DEBUG", enqueue=True, serialize=True, ) logger.debug("Loguru logging configured with stdout and file sinks (stdout=%s)", stdout_level) logging.root.setLevel(logging.DEBUG) except Exception: # Best-effort only — don't crash WSGI startup if logging can't be configured try: logger.add(sys.stderr, level="DEBUG", enqueue=True, backtrace=False, diagnose=False) except Exception: pass logger.debug("Failed to configure Loguru logging sinks, continuing without file logging") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "rad.settings") application = get_wsgi_application()