Use logger in middleware to avoid cluttering the tests with print statements

This commit is contained in:
Klaas van Schelven
2024-04-11 11:19:13 +02:00
parent f098802fde
commit 83eec6854d
2 changed files with 14 additions and 1 deletions

View File

@@ -1,8 +1,11 @@
import logging
from time import time
from django.contrib.auth.decorators import login_required
from django.db import connection
performance_logger = logging.getLogger("bugsink.performance")
class LoginRequiredMiddleware:
def __init__(self, get_response):
@@ -46,5 +49,5 @@ class PerformanceStatsMiddleware:
result = view_func(request, *view_args, **view_kwargs)
took = (time() - t0) * 1000
if took > 1 and not request.path.startswith("/static"):
print(f" {took:.2f}ms / {len(connection.queries)} queries for '{ view_func.__name__ }'")
performance_logger.info(f" {took:.2f}ms / {len(connection.queries)} queries: '{ view_func.__name__ }'")
return result

View File

@@ -1,3 +1,4 @@
from copy import deepcopy
import os
from pathlib import Path
@@ -5,6 +6,8 @@ from pathlib import Path
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
from django.utils.log import DEFAULT_LOGGING
from debug_toolbar.middleware import show_toolbar
# Build paths inside the project like this: BASE_DIR / 'subdir'.
@@ -172,6 +175,13 @@ DEBUG_TOOLBAR_CONFIG = {
}
LOGGING = deepcopy(DEFAULT_LOGGING)
LOGGING['loggers']['bugsink'] = {
"level": "INFO",
"handlers": ["console"],
}
# ###################### SERVER-MODE SETTINGS #################
BUGSINK_DIGEST_IMMEDIATELY = True