Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add File Logging #976

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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://
Expand Down
11 changes: 11 additions & 0 deletions docs/admin/deployment/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
7 changes: 7 additions & 0 deletions ephios/core/apps.py
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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))
2 changes: 0 additions & 2 deletions ephios/core/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
felixrindt marked this conversation as resolved.
Show resolved Hide resolved


def get_all_plugins():
"""
Expand Down
51 changes: 44 additions & 7 deletions ephios/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import copy
import datetime
import os
from datetime import timedelta
from email.utils import getaddresses
Expand Down Expand Up @@ -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": {
Expand All @@ -227,26 +233,57 @@
},
},
"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",
felixrindt marked this conversation as resolved.
Show resolved Hide resolved
},
}

Expand Down