diff --git a/README.md b/README.md index 872dde63..a038e6e1 100644 --- a/README.md +++ b/README.md @@ -344,7 +344,7 @@ If libvirt warns `Host SMBIOS information is not available`, install `dmidecode` | `WEBVIRTCLOUD_DEBUG=1` | Django debug mode: error pages show the traceback and settings; for trying things out only, never on a panel others use | | `WEBVIRTCLOUD_DB_HOST` | use PostgreSQL instead of sqlite (new installs); `WEBVIRTCLOUD_DB_PORT`/`_NAME`/`_USER`/`_PASSWORD` default to 5432/webvirtcloud/webvirtcloud/empty | | `EMAIL_HOST`, `EMAIL_PORT`, `EMAIL_HOST_USER`, `EMAIL_HOST_PASSWORD`, `EMAIL_USE_TLS` | SMTP for e-mail OTP; `EMAIL_USE_TLS` is 1 (STARTTLS, the default) or 0 | -| `WEBVIRTCLOUD_LOG_FILE` | log file (default `data/webvirtcloud.log`, rotated at 10 MB × 5) | +| `WEBVIRTCLOUD_LOG_FILE` | log file (default `data/webvirtcloud.log`; a relative path is from the panel's directory; rotated at 10 MB × 5) | | `SECRET_KEY` | Django secret key (default: the `data/secret_key` file) | | `ADMIN_USERNAME`, `ADMIN_PASSWORD` | the first superuser, read by the first `migrate` only, see [First login](#first-login) | diff --git a/webvirtcloud/settings.py.template b/webvirtcloud/settings.py.template index 99bb9f5e..cdc59720 100644 --- a/webvirtcloud/settings.py.template +++ b/webvirtcloud/settings.py.template @@ -260,10 +260,10 @@ LOCALE_PATHS = [ BASE_DIR / "locale", ] -# Resolve the application log path and make sure its directory exists, so a -# clean checkout, CI, or a Docker build (none of which create data/) does not -# fail when logging is configured. -WEBVIRTCLOUD_LOG_FILE = os.getenv("WEBVIRTCLOUD_LOG_FILE", str(BASE_DIR / "data" / "webvirtcloud.log")) +# Resolve the application log path (a relative one from the panel's directory) +# and make sure its directory exists, so a clean checkout, CI, or a Docker +# build (none of which create data/) does not fail when logging is configured. +WEBVIRTCLOUD_LOG_FILE = str(BASE_DIR / (os.getenv("WEBVIRTCLOUD_LOG_FILE") or "data/webvirtcloud.log")) os.makedirs(os.path.dirname(WEBVIRTCLOUD_LOG_FILE), exist_ok=True) LOGGING = { diff --git a/webvirtcloud/tests/test_settings_env.py b/webvirtcloud/tests/test_settings_env.py index 6533a076..dedf2722 100644 --- a/webvirtcloud/tests/test_settings_env.py +++ b/webvirtcloud/tests/test_settings_env.py @@ -5,6 +5,7 @@ import json import os import subprocess import sys +import tempfile from django.conf import settings from django.test import SimpleTestCase, TestCase, override_settings @@ -164,3 +165,16 @@ class EnvFlagTestCase(SimpleTestCase): for var in self.FLAGS: with self.subTest(var), self.assertRaisesRegex(AssertionError, f"{var} must be 1 or 0, not 'true'"): load_settings({var: "true"}, ["DEBUG"]) + + +class LogFileEnvTestCase(SimpleTestCase): + def log_file(self, env): + return load_settings(env, ["WEBVIRTCLOUD_LOG_FILE"])["WEBVIRTCLOUD_LOG_FILE"] + + def test_a_relative_log_file_is_in_the_panel_directory(self): + self.assertEqual(self.log_file({"WEBVIRTCLOUD_LOG_FILE": "data/panel.log"}), str(settings.BASE_DIR / "data" / "panel.log")) + + def test_the_default_and_an_absolute_log_file(self): + self.assertEqual(self.log_file({"WEBVIRTCLOUD_LOG_FILE": ""}), str(settings.BASE_DIR / "data" / "webvirtcloud.log")) + with tempfile.TemporaryDirectory() as tmp: + self.assertEqual(self.log_file({"WEBVIRTCLOUD_LOG_FILE": f"{tmp}/w.log"}), f"{tmp}/w.log")