mirror of
https://github.com/retspen/webvirtcloud
synced 2026-11-08 19:26:48 +00:00
fix(logging): the application log rotates into a bounded, auto-created directory
This commit is contained in:
parent
bbb9f77cb1
commit
7405217466
3 changed files with 54 additions and 4 deletions
|
|
@ -178,6 +178,12 @@ LOCALE_PATHS = [
|
|||
"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"))
|
||||
os.makedirs(os.path.dirname(WEBVIRTCLOUD_LOG_FILE), exist_ok=True)
|
||||
|
||||
LOGGING = {
|
||||
'version': 1,
|
||||
'disable_existing_loggers': False,
|
||||
|
|
@ -192,10 +198,12 @@ LOGGING = {
|
|||
'class': 'django.utils.log.AdminEmailHandler'
|
||||
},
|
||||
'default': {
|
||||
'level':'INFO',
|
||||
'class':'logging.handlers.RotatingFileHandler',
|
||||
'filename': 'webvirtcloud.log',
|
||||
'formatter':'standard',
|
||||
'level': 'INFO',
|
||||
'class': 'logging.handlers.RotatingFileHandler',
|
||||
'filename': WEBVIRTCLOUD_LOG_FILE,
|
||||
'maxBytes': 10 * 1024 * 1024,
|
||||
'backupCount': 5,
|
||||
'formatter': 'standard',
|
||||
},
|
||||
},
|
||||
'loggers': {
|
||||
|
|
|
|||
40
webvirtcloud/test_logging.py
Normal file
40
webvirtcloud/test_logging.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
"""The app log is bounded (rotates) and written to a deterministic absolute
|
||||
path, not the process working directory."""
|
||||
|
||||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from django.test import TestCase
|
||||
|
||||
|
||||
class LoggingConfigTestCase(TestCase):
|
||||
def test_default_handler_rotates(self):
|
||||
handler = settings.LOGGING["handlers"]["default"]
|
||||
self.assertEqual(handler["class"], "logging.handlers.RotatingFileHandler")
|
||||
self.assertGreater(handler["maxBytes"], 0)
|
||||
self.assertGreater(handler["backupCount"], 0)
|
||||
|
||||
def test_log_path_is_absolute_and_not_cwd(self):
|
||||
handler = settings.LOGGING["handlers"]["default"]
|
||||
self.assertTrue(os.path.isabs(handler["filename"]), handler["filename"])
|
||||
|
||||
def test_missing_log_directory_is_created_on_settings_import(self):
|
||||
# A clean checkout / CI / Docker build may not have the log's parent dir;
|
||||
# importing settings (logging config) must create it, not crash.
|
||||
import subprocess
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
tmp = tempfile.mkdtemp()
|
||||
log = os.path.join(tmp, "nested", "wvc.log")
|
||||
env = {
|
||||
**os.environ,
|
||||
"WEBVIRTCLOUD_LOG_FILE": log,
|
||||
"DJANGO_SETTINGS_MODULE": "webvirtcloud.settings",
|
||||
}
|
||||
result = subprocess.run(
|
||||
[sys.executable, "-c", "import django; django.setup()"],
|
||||
env=env, capture_output=True, text=True,
|
||||
)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertTrue(os.path.isdir(os.path.dirname(log)), log)
|
||||
Loading…
Add table
Add a link
Reference in a new issue