最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - I have added a custom loggin to my django project but it isn't working - Stack Overflow

programmeradmin1浏览0评论

I have added this code to my settings.py, but it's not working (It's not logging errors warnings and ...). 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.")
发布评论

评论列表(0)

  1. 暂无评论