GNOME-Nepal / site-server

Backend server for GNOME Nepal
MIT License
6 stars 7 forks source link

feat(log-path): log path is defined in the .env #52

Closed rex9840 closed 2 weeks ago

rex9840 commented 2 weeks ago

@PublisherName BASE_DIR is django project dir and as for the log_dir exitst=true does the validaton for us as per the expections it only occurs with logs not the dir do we really need it

PublisherName commented 2 weeks ago

@rex9840 If LOG_PATH is set to a restricted folder like /var/www/logs, there’s a good chance we’ll run into permission issues. Although Django can catch this, it’s helpful to wrap the setup in a try block. Even better, if a permission error happens, we can fall back to using a safe location like /tmp/logs or BASE_DIR / logs instead. This way, we don’t need to worry about permission errors stopping the logs from being saved.

default_log_dir = BASE_DIR / "logs"
log_path = os.getenv("LOG_PATH", default_log_dir)
LOG_DIR = Path(log_path)

try:
    LOG_DIR.mkdir(exist_ok=True, parents=True)
    if not os.access(LOG_DIR, os.W_OK):
        raise PermissionError(f"No write permission for log directory: {LOG_DIR}")
except PermissionError as e:
    LOG_DIR = Path("/tmp/logs") # Or Path(default_log_dir)
    LOG_DIR.mkdir(exist_ok=True, parents=True)
    print(f"Warning: Using fallback log directory at {LOG_DIR} due to: {e}")
except Exception as e:
    raise RuntimeError(f"Failed to set up log directory: {e}")
n1rjal commented 2 weeks ago

@PublisherName we deploy in platform like open shift or any kubernetes based environment where we don't get enough permissions even to temp folder unless we mount it. So, we will be happy with raising exceptions.

It's only for openshift environment though your use case works for majority of enivironment.