Repository: bookwyrm-social/bookwyrm

Issue:
We should be logging 500 errors in prod
**Is your feature request related to a problem? Please describe.**
By default Django doesn't log 500 errors in prod, we should figure out how to make that happen

**Describe the solution you'd like**
I would like information about 500 errors logged to the docker log

**Describe alternatives you've considered**
This is probably accomplishable more generally by making the log level more verbose (see #1787) but it would be nice to have 500s logged by default, regardless.

(this is mostly a note-to-self)



Discussion:

Resolving patch:
diff --git a/bookwyrm/settings.py b/bookwyrm/settings.py
--- a/bookwyrm/settings.py
+++ b/bookwyrm/settings.py
@@ -106,6 +106,58 @@
     },
 ]
 
+LOG_LEVEL = env("LOG_LEVEL", "INFO").upper()
+# Override aspects of the default handler to our taste
+# See https://docs.djangoproject.com/en/3.2/topics/logging/#default-logging-configuration
+# for a reference to the defaults we're overriding
+#
+# It seems that in order to override anything you have to include its
+# entire dependency tree (handlers and filters) which makes this a
+# bit verbose
+LOGGING = {
+    "version": 1,
+    "disable_existing_loggers": False,
+    "filters": {
+        # These are copied from the default configuration, required for
+        # implementing mail_admins below
+        "require_debug_false": {
+            "()": "django.utils.log.RequireDebugFalse",
+        },
+        "require_debug_true": {
+            "()": "django.utils.log.RequireDebugTrue",
+        },
+    },
+    "handlers": {
+        # Overrides the default handler to make it log to console
+        # regardless of the DEBUG setting (default is to not log to
+        # console if DEBUG=False)
+        "console": {
+            "level": LOG_LEVEL,
+            "class": "logging.StreamHandler",
+        },
+        # This is copied as-is from the default logger, and is
+        # required for the django section below
+        "mail_admins": {
+            "level": "ERROR",
+            "filters": ["require_debug_false"],
+            "class": "django.utils.log.AdminEmailHandler",
+        },
+    },
+    "loggers": {
+        # Install our new console handler for Django's logger, and
+        # override the log level while we're at it
+        "django": {
+            "handlers": ["console", "mail_admins"],
+            "level": LOG_LEVEL,
+        },
+        # Add a bookwyrm-specific logger
+        "bookwyrm": {
+            "handlers": ["console"],
+            "level": LOG_LEVEL,
+        },
+    },
+}
+
 
 WSGI_APPLICATION = "bookwyrm.wsgi.application"
 
diff --git a/bookwyrm/views/inbox.py b/bookwyrm/views/inbox.py
--- a/bookwyrm/views/inbox.py
+++ b/bookwyrm/views/inbox.py
@@ -1,7 +1,10 @@
 """ incoming activities """
 import json
 import re
+import logging
+
 from urllib.parse import urldefrag
+import requests
 
 from django.http import HttpResponse, Http404
 from django.core.exceptions import BadRequest, PermissionDenied
@@ -9,13 +12,14 @@
 from django.utils.decorators import method_decorator
 from django.views import View
 from django.views.decorators.csrf import csrf_exempt
-import requests
 
 from bookwyrm import activitypub, models
 from bookwyrm.tasks import app
 from bookwyrm.signatures import Signature
 from bookwyrm.utils import regex
 
+logger = logging.getLogger(__name__)
+
 
 @method_decorator(csrf_exempt, name="dispatch")
 # pylint: disable=no-self-use
@@ -71,6 +75,7 @@ def raise_is_blocked_user_agent(request):
         return
     url = url.group()
     if models.FederatedServer.is_blocked(url):
+        logger.debug("%s is blocked, denying request based on user agent", url)
         raise PermissionDenied()
 
 
@@ -78,16 +83,18 @@ def raise_is_blocked_activity(activity_json):
     """get the sender out of activity json and check if it's blocked"""
     actor = activity_json.get("actor")
 
+    if not actor:
+        # well I guess it's not even a valid activity so who knows
+        return
+
     # check if the user is banned/deleted
     existing = models.User.find_existing_by_remote_id(actor)
     if existing and existing.deleted:
+        logger.debug("%s is banned/deleted, denying request based on actor", actor)
         raise PermissionDenied()
 
-    if not actor:
-        # well I guess it's not even a valid activity so who knows
-        return
-
     if models.FederatedServer.is_blocked(actor):
+        logger.debug("%s is blocked, denying request based on actor", actor)
         raise PermissionDenied()
 
 


Test patch: