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_false": {
            "()": "django.utils.log.RequireDebugFalse",
        },
        "require_debug_true": {
            "()": "django.utils.log.RequireDebugTrue",
        },
        "skip_variable_doesnotexist": {
            "()": "evibes.settings.logconfig.SkipVariableDoesNotExistFilter",
        },
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "color",
        },
        "mail_admins": {
            "level": "ERROR",
            "filters": ["require_debug_false"],
            "class": "django.utils.log.AdminEmailHandler",
        },
    },
    "loggers": {
        "django": {
            "handlers": [
                "console",
                "mail_admins",
            ],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": True,
        },
        "django.server": {
            "level": "INFO",
            "propagate": False,
        },
        "django.db.backends": {
            "handlers": [
                "console",
                "mail_admins",
            ],
            "level": "WARNING",
            "propagate": False,
        },
        "django.template": {
            "handlers": [
                "console",
                "mail_admins",
            ],
            "level": "DEBUG" if DEBUG else "ERROR",
            "propagate": True,
            "filters": ["skip_variable_doesnotexist"],
        },
        "uvicorn.access": {
            "handlers": [
                "console",
            ],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": False,
        },
        "uvicorn.error": {
            "handlers": [
                "console",
            ],
            "level": "WARNING",
            "propagate": False,
        },
        "django_elasticsearch_dsl": {
            "handlers": [
                "console",
            ],
            "level": "WARNING",
            "propagate": False,
        },
        "celery.app.trace": {
            "handlers": [
                "console",
            ],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": False,
        },
        "celery.worker.strategy": {
            "handlers": [
                "console",
            ],
            "level": "DEBUG" if DEBUG else "INFO",
            "propagate": False,
        },
        "elastic_transport.transport": {
            "handlers": [
                "console",
            ],
            "level": "ERROR",
            "propagate": False,
        },
    },
}
