🔊 add logging module to nanodjango app

This commit is contained in:
Marc Koch 2025-09-22 11:03:40 +02:00
parent 8d54361db5
commit fbe9d99976
Signed by: marc
GPG Key ID: 12406554CFB028B9
1 changed files with 21 additions and 14 deletions

View File

@ -1,3 +1,4 @@
import logging
import os import os
import secrets import secrets
from datetime import datetime from datetime import datetime
@ -17,21 +18,28 @@ from ics import Calendar as ICS_Calendar, Event as ICS_Event
from nanodjango import Django from nanodjango import Django
from shortuuid.django_fields import ShortUUIDField from shortuuid.django_fields import ShortUUIDField
from mylogger.logger import setup_logging
DEBUG = os.getenv("DJANGO_DEBUG", False) DEBUG = os.getenv("DJANGO_DEBUG", False)
SECRET_KEY = os.getenv("DJANGO_SECRET_KEY") \ SECRET_KEY = os.getenv("DJANGO_SECRET_KEY") \
if os.getenv("DJANGO_SECRET_KEY") \ if os.getenv("DJANGO_SECRET_KEY") \
else secrets.token_hex(40) if DEBUG else None else secrets.token_hex(40) if DEBUG else None
BASE_DIR = Path(__file__).resolve().parent BASE_DIR = Path(__file__).resolve().parent
DATA_DIR = Path(os.getenv("DJANGO_DATA_DIR", BASE_DIR.parent / "data")) DATA_DIR = Path(os.getenv("DJANGO_DATA_DIR", "/data"))
FILE_LOG_LEVEL = os.getenv("FILE_LOG_LEVEL", "INFO")
STDOUT_LOG_LEVEL = os.getenv("STDOUT_LOG_LEVEL", "DEBUG")
LOG_DIR = os.getenv("LOG_DIR", "/data/room-booking.log.jsonl")
LOGROTATE_BACKUP_COUNT = int(os.getenv("LOGROTATE_BACKUP_COUNT", 6))
LOGROTATE_MAX_BYTES = int(os.getenv("LOGROTATE_MAX_BYTES", 10 * 1024 * 1024))
if DEBUG: setup_logging(file_log_level=FILE_LOG_LEVEL,
print(f"DEBUG mode is {'on' if DEBUG else 'off'}") stdout_log_level=STDOUT_LOG_LEVEL,
print(f"BASE_DIR: {BASE_DIR}") logdir=LOG_DIR)
print(f"DATA_DIR: {DATA_DIR}") logger = logging.getLogger(__name__)
# Check if all required values are set # Check if all required values are set
if not SECRET_KEY and not DEBUG: if not SECRET_KEY:
print("DJANGO_SECRET_KEY is not set") logger.exception("DJANGO_SECRET_KEY is not set")
exit(1) exit(1)
ALLOWED_HOSTS = [host.strip() for host in ALLOWED_HOSTS = [host.strip() for host in
@ -44,9 +52,8 @@ CSRF_TRUSTED_ORIGINS = [
for host in ALLOWED_HOSTS for host in ALLOWED_HOSTS
] ]
if DEBUG: logger.debug(f"ALLOWED_HOSTS: {ALLOWED_HOSTS}")
print(f"ALLOWED_HOSTS: {ALLOWED_HOSTS}") logger.debug(f"CSRF_TRUSTED_ORIGINS: {CSRF_TRUSTED_ORIGINS}")
print(f"CSRF_TRUSTED_ORIGINS: {CSRF_TRUSTED_ORIGINS}")
# Initialise nanodjango # Initialise nanodjango
app = Django( app = Django(
@ -292,7 +299,7 @@ class PriorityEventToken(models.Model):
try: try:
uuid = UUID(uuid, version=4) uuid = UUID(uuid, version=4)
except ValueError: except ValueError:
print(f"Failed to decode UUID {uuid}") logger.exception(f"Failed to decode UUID {uuid}")
return False return False
if not self.is_used: if not self.is_used:
@ -311,11 +318,11 @@ class PriorityEventToken(models.Model):
try: try:
uuid = UUID(uuid, version=4) uuid = UUID(uuid, version=4)
except ValueError: except ValueError:
print(f"Failed to decode UUID {uuid}") logger.exception(f"Failed to decode UUID {uuid}")
return False return False
if not self.signature: if not self.signature:
print(f"No signature found for token '{self.token}'") logger.exception(f"No signature found for token '{self.token}'")
return False return False
return self._validate(uuid, self.signature) return self._validate(uuid, self.signature)
@ -339,7 +346,7 @@ class PriorityEventToken(models.Model):
try: try:
return signer.unsign(signature) == uuid.__str__() return signer.unsign(signature) == uuid.__str__()
except BadSignature as e: except BadSignature as e:
print(e) logger.exception(f"Bad signature: {e}")
return False return False
def __str__(self): def __str__(self):