From 11308dc2bcbefeed53c49f1c31f7d0686ea81340 Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Wed, 13 May 2020 12:46:21 +0200 Subject: [PATCH 1/2] misc: remove LoggingCollectorMiddleware (#42821) --- src/authentic2/middleware.py | 77 ------------------------------- src/authentic2/settings.py | 1 - src/authentic2/templates/500.html | 18 -------- 3 files changed, 96 deletions(-) diff --git src/authentic2/middleware.py src/authentic2/middleware.py index 7e023756..e9bf270f 100644 --- src/authentic2/middleware.py +++ src/authentic2/middleware.py @@ -14,8 +14,6 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -import logging -import datetime import random import struct import time @@ -36,81 +34,6 @@ from django.shortcuts import render from . import app_settings, utils, plugins -class ThreadCollector(object): - def __init__(self): - if threading is None: - raise NotImplementedError( - "threading module is not available, " - "this panel cannot be used without it") - self.collections = {} # a dictionary that maps threads to collections - - def get_collection(self, thread=None): - """ - Returns a list of collected items for the provided thread, of if none - is provided, returns a list for the current thread. - """ - if thread is None: - thread = threading.currentThread() - if thread not in self.collections: - self.collections[thread] = [] - return self.collections[thread] - - def clear_collection(self, thread=None): - if thread is None: - thread = threading.currentThread() - if thread in self.collections: - del self.collections[thread] - - def collect(self, item, thread=None): - self.get_collection(thread).append(item) - -MESSAGE_IF_STRING_REPRESENTATION_INVALID = '[Could not get log message]' - - -class ThreadTrackingHandler(logging.Handler): - def __init__(self, collector): - logging.Handler.__init__(self) - self.collector = collector - - def emit(self, record): - try: - message = self.format(record) - except Exception: - message = MESSAGE_IF_STRING_REPRESENTATION_INVALID - - record = { - 'message': message, - 'time': datetime.datetime.fromtimestamp(record.created), - 'level': record.levelname, - 'file': record.pathname, - 'line': record.lineno, - 'channel': record.name, - } - self.collector.collect(record) - - -# We don't use enable/disable_instrumentation because logging is global. -# We can't add thread-local logging handlers. Hopefully logging is cheap. - -collector = ThreadCollector() -logging_handler = ThreadTrackingHandler(collector) -logging.root.addHandler(logging_handler) - - -class LoggingCollectorMiddleware(MiddlewareMixin): - def process_request(self, request): - collector.clear_collection() - - def show_logs(self, request): - if request.META.get('REMOTE_ADDR', None) in settings.INTERNAL_IPS: - return True - - def process_exception(self, request, exception): - if self.show_logs(request): - request.logs = collector.get_collection() - request.exception = exception - - class CollectIPMiddleware(MiddlewareMixin): def process_response(self, request, response): # only collect IP if session is used diff --git src/authentic2/settings.py src/authentic2/settings.py index 8892cf19..a5d25380 100644 --- src/authentic2/settings.py +++ src/authentic2/settings.py @@ -90,7 +90,6 @@ TEMPLATES = [ MIDDLEWARE = ( 'authentic2.middleware.StoreRequestMiddleware', 'authentic2.middleware.RequestIdMiddleware', - 'authentic2.middleware.LoggingCollectorMiddleware', 'authentic2.middleware.ServiceAccessControlMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.http.ConditionalGetMiddleware', diff --git src/authentic2/templates/500.html src/authentic2/templates/500.html index ee2608d2..3f2d9ec1 100644 --- src/authentic2/templates/500.html +++ src/authentic2/templates/500.html @@ -9,22 +9,4 @@

{% trans "We're sorry but a server error has occurred." %}

-{% if request.logs %} -

Logs

- - {% for log in request.logs %} - - - - - - {% endfor %} -
{{ log.time|time:"H:i:s" }}{{ log.level }}{{ log.message }}
-{% endif %} - -{% if request.exception %} -

Exception

-

{{ request.exception }}

-{% endif %} - {% endblock %} -- 2.26.0