[Python] I have added a custom loggin to my django project but it isn't working

Discussão em 'Python' iniciado por Stack, Março 13, 2025.

  1. Stack

    Stack Membro Participativo

    I have added this code to my settings.py, but it's not working. Django default logging works perfectly fine, debug is True in my settings and I've created logs folder manually.

    LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
    'file_logging': {
    'format': '{levelname} / {levelno} - {asctime} --- {pathname} in {lineno} --- {process:d} {thread:d} -- {message}',
    'style': '{'
    },
    'email_logging': {
    'format': '{levelname} at {asctime} --- {pathname} in {lineno} -- {message}',
    'style': '{'
    },
    },
    'filters': {
    'debug_true_required': {
    '()': 'django.utils.log.RequireDebugTrue'
    }
    },
    'handlers': {
    'full_handler': {
    'level': 'INFO',
    'class': 'logging.FileHandler',
    'filename': 'logs/full.log',
    'formatter': 'file_logging',
    },
    'critical_handler': {
    'level': 'CRITICAL',
    'class': 'logging.FileHandler',
    'filename': 'logs/critical.log',
    'formatter': 'file_logging',
    },
    'error_handler': {
    'level': 'ERROR',
    'filters': ['debug_true_required',],
    'class': 'logging.FileHandler',
    'filename': 'logs/error.log',
    'formatter': 'file_logging',
    },
    'critical_email_handler': {
    'level': 'CRITICAL',
    'class': 'django.utils.log.AdminEmailHandler',
    'formatter': 'email_logging',
    },
    'error_email_handler': {
    'level': 'ERROR',
    'filters': ['debug_true_required',],
    'class': 'django.utils.log.AdminEmailHandler',
    'formatter': 'email_logging',
    },
    },
    'loggers': {
    'file_logger': {
    'handlers': ['full_handler', 'critical_handler', 'error_handler'],
    'level': 'INFO',
    'propagate': True
    },
    'email_logger': {
    'handlers': ['critical_email_handler', 'error_email_handler'],
    'level': 'ERROR',
    'propagate': True
    },
    }
    }


    I tried recreating logs folder again, but it didn't work. I then tried to change 'propagate': False to True but it didn't work again. I also tried logging manualy with this code, which works perfectly fine, and the logs saved correctly in my logging files.

    logger = logging.getLogger('file_logger')
    logger.info("This is a test log message.")

    logger = logging.getLogger('email_logger')
    logger.critical("This is a test critical message.")

    Continue reading...

Compartilhe esta Página