schon/evibes/settings/logconfig.py
Egor fureunoir Gorbunov adfffee0a3 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.
2025-07-06 16:58:43 +03:00

129 lines
4 KiB
Python

import logging
from evibes.settings.base import DEBUG
class SkipVariableDoesNotExistFilter(logging.Filter):
def filter(self, record: logging.LogRecord) -> bool:
if record.exc_info:
exc_type, exc_instance, _ = record.exc_info
try:
if exc_type is not None:
if exc_type.__name__ == "VariableDoesNotExist":
return False
else:
return True
except AttributeError:
return True
return "VariableDoesNotExist" not in record.getMessage()
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"color": {
"()": "colorlog.ColoredFormatter",
"format": "%(asctime)s %(log_color)s[%(levelname)s]%(reset)s %(name)s: %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S",
"log_colors": {
"DEBUG": "cyan",
"INFO": "bold_green",
"WARNING": "bold_yellow",
"ERROR": "bold_red",
"CRITICAL": "bold_red,bg_white",
},
},
"plain": {
"format": "[%(asctime)s] [%(levelname)s] %(name)s: %(message)s",
"datefmt": "%Y-%m-%d %H:%M:%S",
},
},
"filters": {
"require_debug_true": {
"()": "django.utils.log.RequireDebugTrue",
},
"skip_variable_doesnotexist": {
"()": "evibes.settings.logconfig.SkipVariableDoesNotExistFilter",
},
},
"handlers": {
"console_debug": {
"level": "DEBUG",
"filters": ["require_debug_true"],
"class": "logging.StreamHandler",
"formatter": "color",
},
"console_production": {
"level": "WARNING",
"class": "logging.StreamHandler",
"formatter": "color",
},
},
"loggers": {
"django": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO", # noqa: F405
"propagate": True,
},
"django.request": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
},
"django.db.backends": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "WARNING",
"propagate": False,
},
"django.template": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": True,
"filters": ["skip_variable_doesnotexist"],
},
"evibes": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "WARNING",
"propagate": True,
},
"django_elasticsearch_dsl": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "WARNING",
"propagate": False,
},
"celery.app.trace": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
},
"celery.worker.strategy": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "DEBUG" if DEBUG else "INFO",
"propagate": False,
},
"elastic_transport.transport": {
"handlers": [
"console_debug" if DEBUG else "console_production",
],
"level": "ERROR",
"propagate": False,
},
},
}