繁体   English   中英

Django 日志记录 - 登录 ValidationError 不起作用

[英]Django logging - logging on ValidationError not working

所以我希望能够跟踪由某种形式的 def clean 方法引发的任何 ValidationErrors。 我有一个名为 AdForm 的表单,表单上的验证确实有效,我可以在 html 输出中看到错误。

日志记录也有效,因为我在文本文件中获取了记录器信息。 奇怪的是,我收到了通知;

2020-02-11 18:17:43,099 [INFO] .forms:AdForm 被调用

但是我在日志文件中没有看到引发的验证错误?

我的设置有问题吗? 对于你们中的一些人来说,这可能是小菜一碟,但我正在努力寻找自己的错误,任何帮助将不胜感激!

表格.py

from .models import Ad

import logging
logger = logging.getLogger(__name__)

class AdForm(forms.ModelForm)
    logger.info("AdForm is called")

    class Meta:
        model = Ad
        fields = [
            'title',
            'description'
        ]

    def clean(self):
        cleaned_date = super(AdForm, self).clean()
        title = cleaned_data.get('title')

        if len(title) < 4:
            logger.info("Title is shorter then 4 characters")
            raise forms.ValidationError("Title is too short")

        if len(title) > 24:
            logger.info("Title is shorter then 4 characters")
            raise forms.ValidationError("Title is too long")

设置.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'C:/Python/logs/mylog.log',
            'maxBytes': 1024*1024*5, #5mb
            'backupCount': 5,
            'formatter': 'standard',
        },
        'request_handler': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': 'C:/Python/logs/django_request.log',
            'maxBytes': 1024*1024*5, #5mb
            'backupCount': 5,
            'formatter': 'standard',
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
    }
}

好的,在日志设置中发现了一个问题;

LOGGING = {
    'disable_existing_loggers': False, # Change this to False
}

这似乎可以解决问题!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM