Merge pull request #1802 from cincodenada/configurable-logging

Make log level configurable, override default logging config
This commit is contained in:
Mouse Reeve 2022-01-18 07:39:43 -08:00 committed by GitHub
commit 9e450f2d99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 65 additions and 6 deletions

View file

@ -106,6 +106,58 @@ TEMPLATES = [
},
]
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"

View file

@ -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.shortcuts import get_object_or_404
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")
# check if the user is banned/deleted
existing = models.User.find_existing_by_remote_id(actor)
if existing and existing.deleted:
raise PermissionDenied()
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 models.FederatedServer.is_blocked(actor):
logger.debug("%s is blocked, denying request based on actor", actor)
raise PermissionDenied()