Features: 1) Update all log handlers to dynamically switch based on DEBUG setting; 2) Introduce improved middleware logging configuration.

Fixes: 1) Remove unnecessary wildcard imports in settings files; 2) Add missing imports for `getenv`, `DEBUG`, and other constants; 3) Fix logger usage in middleware to align with `django.request`.

Extra: 1) Refactor settings files for readability and consistency; 2) Remove outdated `noqa` comments; 3) Minor formatting adjustments.
This commit is contained in:
Egor Pavlovich Gorbunov 2025-07-06 16:58:43 +03:00
parent 41dd02147c
commit adfffee0a3
8 changed files with 51 additions and 33 deletions

View file

@ -16,7 +16,7 @@ from sentry_sdk import capture_exception
from evibes.settings import DEBUG from evibes.settings import DEBUG
logger = logging.getLogger("evibes") logger = logging.getLogger("django.request")
class CustomCommonMiddleware(CommonMiddleware): class CustomCommonMiddleware(CommonMiddleware):

View file

@ -1,4 +1,4 @@
from evibes.settings.base import getenv from os import getenv
DATABASES = { DATABASES = {
"default": { "default": {

View file

@ -1,4 +1,4 @@
from evibes.settings.base import getenv from os import getenv
DBBACKUP_CONNECTORS = { DBBACKUP_CONNECTORS = {
"default": { "default": {

View file

@ -1,9 +1,9 @@
# mypy: ignore-errors
from datetime import timedelta from datetime import timedelta
from os import getenv
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from evibes.settings.base import * # noqa: F403 from evibes.settings import DEBUG, EVIBES_VERSION, SECRET_KEY
from evibes.settings.constance import CONSTANCE_CONFIG from evibes.settings.constance import CONSTANCE_CONFIG
REST_FRAMEWORK: dict = { REST_FRAMEWORK: dict = {

View file

@ -1,9 +1,11 @@
from evibes.settings.base import * # noqa: F403 from os import getenv
from evibes.settings import DEBUG
ELASTICSEARCH_DSL = { ELASTICSEARCH_DSL = {
"default": { "default": {
"hosts": ["http://elasticsearch:9200"], "hosts": ["http://elasticsearch:9200"],
"http_auth": ("elastic", getenv("ELASTIC_PASSWORD")), # noqa: F405 "http_auth": ("elastic", getenv("ELASTIC_PASSWORD")),
"verify_certs": False, "verify_certs": False,
"timeout": 30, "timeout": 30,
"ssl_show_warn": False, "ssl_show_warn": False,
@ -12,6 +14,6 @@ ELASTICSEARCH_DSL = {
}, },
} }
ELASTICSEARCH_DSL_AUTOSYNC = DEBUG # noqa: F405 ELASTICSEARCH_DSL_AUTOSYNC = DEBUG
ELASTICSEARCH_DSL_PARALLEL = True ELASTICSEARCH_DSL_PARALLEL = True
ELASTICSEARCH_DSL_SIGNAL_PROCESSOR = "django_elasticsearch_dsl.signals.CelerySignalProcessor" ELASTICSEARCH_DSL_SIGNAL_PROCESSOR = "django_elasticsearch_dsl.signals.CelerySignalProcessor"

View file

@ -1,3 +1,5 @@
from evibes.settings.base import * # noqa: F403
GRAPH_MODELS = { GRAPH_MODELS = {
"all_applications": True, "all_applications": True,
"group_models": True, "group_models": True,

View file

@ -1,11 +1,11 @@
from evibes.settings.base import * # noqa: F403 from evibes.settings.base import DEBUG
GRAPHENE = { GRAPHENE = {
"MIDDLEWARE": [ "MIDDLEWARE": [
"evibes.middleware.GrapheneLoggingErrorsDebugMiddleware", "evibes.middleware.GrapheneLoggingErrorsDebugMiddleware",
"evibes.middleware.GrapheneJWTAuthorizationMiddleware", "evibes.middleware.GrapheneJWTAuthorizationMiddleware",
] ]
if DEBUG # noqa: F405 if DEBUG
else [ else [
"evibes.middleware.GrapheneJWTAuthorizationMiddleware", "evibes.middleware.GrapheneJWTAuthorizationMiddleware",
], ],

View file

@ -1,8 +1,10 @@
from evibes.settings.base import * # noqa: F403 import logging
from evibes.settings.base import DEBUG
class SkipVariableDoesNotExistFilter(logging.Filter): # noqa: F405 class SkipVariableDoesNotExistFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool: # noqa: F405 def filter(self, record: logging.LogRecord) -> bool:
if record.exc_info: if record.exc_info:
exc_type, exc_instance, _ = record.exc_info exc_type, exc_instance, _ = record.exc_info
try: try:
@ -57,57 +59,69 @@ LOGGING = {
"class": "logging.StreamHandler", "class": "logging.StreamHandler",
"formatter": "color", "formatter": "color",
}, },
"mail_admins": {
"level": "ERROR",
"class": "django.utils.log.AdminEmailHandler",
"include_html": True,
"formatter": "plain",
},
}, },
"loggers": { "loggers": {
"django": { "django": {
"handlers": ["console_debug", "console_production"], "handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO", # noqa: F405 "level": "DEBUG" if DEBUG else "INFO", # noqa: F405
"propagate": True, "propagate": True,
}, },
"django.request": { "django.request": {
"handlers": ["console_debug", "mail_admins"], "handlers": [
"level": "DEBUG" if DEBUG else "INFO", # noqa: F405 "console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False, "propagate": False,
}, },
"django.db.backends": { "django.db.backends": {
"handlers": ["console_debug" if DEBUG else "console_production"], # noqa: F405 "handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "WARNING", "level": "WARNING",
"propagate": False, "propagate": False,
}, },
"django.template": { "django.template": {
"handlers": ["console_debug", "console_production"], "handlers": [
"level": "DEBUG" if DEBUG else "INFO", # noqa: F405 "console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": True, "propagate": True,
"filters": ["skip_variable_doesnotexist"], "filters": ["skip_variable_doesnotexist"],
}, },
"evibes": { "evibes": {
"handlers": ["console_debug" if DEBUG else "console_production"], # noqa: F405 "handlers": [
"level": "DEBUG" if DEBUG else "WARNING", # noqa: F405 "console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "WARNING",
"propagate": True, "propagate": True,
}, },
"django_elasticsearch_dsl": { "django_elasticsearch_dsl": {
"handlers": ["console_debug" if DEBUG else "console_production"], # noqa: F405 "handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "WARNING", "level": "WARNING",
"propagate": False, "propagate": False,
}, },
"celery.app.trace": { "celery.app.trace": {
"handlers": ["console_debug" if DEBUG else "console_production"], # noqa: F405 "handlers": [
"level": "DEBUG" if DEBUG else "INFO", # noqa: F405 "console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False, "propagate": False,
}, },
"celery.worker.strategy": { "celery.worker.strategy": {
"handlers": ["console_debug" if DEBUG else "console_production"], # noqa: F405 "handlers": [
"level": "DEBUG" if DEBUG else "INFO", # noqa: F405 "console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False, "propagate": False,
}, },
"elastic_transport.transport": { "elastic_transport.transport": {
"handlers": ["console_debug" if DEBUG else "console_production"], # noqa: F405 "handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "ERROR", "level": "ERROR",
"propagate": False, "propagate": False,
}, },