From bf7869e042301240bf75651c03e21bf974e79e97 Mon Sep 17 00:00:00 2001 From: Felix Rindt Date: Fri, 16 Jun 2023 15:20:10 +0200 Subject: [PATCH 1/2] set default log level to INFO --- ephios/core/apps.py | 7 +++++++ ephios/core/plugins.py | 2 -- ephios/settings.py | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ephios/core/apps.py b/ephios/core/apps.py index f51866d3f..49d1ffd7b 100644 --- a/ephios/core/apps.py +++ b/ephios/core/apps.py @@ -1,6 +1,11 @@ +import logging + from django.apps import AppConfig +from django.conf import settings from dynamic_preferences.registries import preference_models +logger = logging.getLogger(__name__) + class CoreAppConfig(AppConfig): name = "ephios.core" @@ -12,3 +17,5 @@ def ready(self): EventTypePreference = self.get_model("EventTypePreference") preference_models.register(EventTypePreference, event_type_preference_registry) + + logger.info("Installed plugins: %s", ", ".join(settings.PLUGINS)) diff --git a/ephios/core/plugins.py b/ephios/core/plugins.py index 5a24233f1..4ea755945 100644 --- a/ephios/core/plugins.py +++ b/ephios/core/plugins.py @@ -11,8 +11,6 @@ # The plugin mechanics are heavily inspired by pretix (then licenced under Apache 2.0) - Check it out! -logger.info("Installed plugins: %s", ", ".join(settings.PLUGINS)) - def get_all_plugins(): """ diff --git a/ephios/settings.py b/ephios/settings.py index b4b5b7dff..5b14fe197 100644 --- a/ephios/settings.py +++ b/ephios/settings.py @@ -247,6 +247,7 @@ }, "root": { "handlers": ["mail_admins", "console"], + "level": "INFO", }, } From ae1f6217b0de35b635827aabe83ca9dcf3261d7e Mon Sep 17 00:00:00 2001 From: Felix Rindt Date: Wed, 2 Aug 2023 23:56:16 +0200 Subject: [PATCH 2/2] add file logging --- .env.example | 1 + docs/admin/deployment/index.rst | 11 ++++++++ ephios/settings.py | 50 ++++++++++++++++++++++++++++----- 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/.env.example b/.env.example index 293fbe4f2..74fc81b75 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,7 @@ DATABASE_URL=sqlite:///data/db.sqlite3 ALLOWED_HOSTS="*" STATIC_URL=/static/ STATIC_ROOT=data/static/ +LOGGING_FILE=data/logs/ephios.log INTERNAL_IPS="127.0.0.1" SITE_URL=http://localhost:8000 EMAIL_URL=dummymail:// diff --git a/docs/admin/deployment/index.rst b/docs/admin/deployment/index.rst index f7b04ae4e..9e669dcd9 100644 --- a/docs/admin/deployment/index.rst +++ b/docs/admin/deployment/index.rst @@ -8,3 +8,14 @@ This section shows how to deploy ephios in a production environment. .. toctree:: :maxdepth: 2 + + +Logging +------- + +In production, ephios logs to a file at `LOGGING_FILE` configured in the environment. +The file is rotated daily, copies are kept for `LOGGING_BACKUP_DAYS` days (default 14). + +Also, ephios sends emails to the `ADMINS` configured in the environment on errors. + +Feel free to further specify the `LOGGING` dict in your own django settings file. \ No newline at end of file diff --git a/ephios/settings.py b/ephios/settings.py index 5b14fe197..31131d532 100644 --- a/ephios/settings.py +++ b/ephios/settings.py @@ -1,4 +1,5 @@ import copy +import datetime import os from datetime import timedelta from email.utils import getaddresses @@ -212,11 +213,16 @@ ADMINS = getaddresses([env("ADMINS")]) # logging +LOGGING_FILE = env.str("LOGGING_FILE", default=None) +use_file_logging = not DEBUG and LOGGING_FILE is not None +if use_file_logging: + Path(LOGGING_FILE).parent.mkdir(parents=True, exist_ok=True) + LOGGING = { "version": 1, "disable_existing_loggers": False, "formatters": { - "default": {"format": "%(levelname)s %(asctime)s %(name)s %(module)s %(message)s"}, + "default": {"format": "[%(levelname)s] %(asctime)s %(name)s :: %(message)s"}, }, "filters": { "require_debug_false": { @@ -227,26 +233,56 @@ }, }, "handlers": { + "console": { + "level": "DEBUG", + "class": "logging.StreamHandler", + "formatter": "default", + "filters": ["require_debug_true"], + }, "mail_admins": { "level": "ERROR", "filters": ["require_debug_false"], "class": "django.utils.log.AdminEmailHandler", }, - "console": { - "level": "INFO", - "class": "logging.StreamHandler", + "file": { + "level": "DEBUG", "formatter": "default", + "filters": ["require_debug_false"], + **( + { + "class": "logging.handlers.TimedRotatingFileHandler", + "filename": LOGGING_FILE, + "when": "midnight", + "backupCount": env.int("LOGGING_BACKUP_DAYS", default=14), + "atTime": datetime.time(4), + "encoding": "utf-8", + } + if use_file_logging + else { + "class": "logging.NullHandler", + } + ), }, }, "loggers": { + "ephios": { + "handlers": ["mail_admins", "console", "file"], + "level": "DEBUG" if DEBUG else "INFO", + "propagate": False, + }, "django": { - "handlers": ["mail_admins", "console"], + "handlers": [], "level": "INFO", - "propagate": False, + "propagate": True, + }, + "django.server": { + "handlers": [], + "level": "INFO", + "propagate": True, }, }, "root": { - "handlers": ["mail_admins", "console"], + "handlers": ["mail_admins", "console", "file"], "level": "INFO", }, }