简体   繁体   English

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

[英]Django logging - logging on ValidationError not working

So I want to be able to track any ValidationErrors being raised by the def clean method on a certain form.所以我希望能够跟踪由某种形式的 def clean 方法引发的任何 ValidationErrors。 I have the a form called AdForm, the validation on the form does work, I can see the errors in the html output.我有一个名为 AdForm 的表单,表单上的验证确实有效,我可以在 html 输出中看到错误。

The logging also works since I get the logger info in the text file.日志记录也有效,因为我在文本文件中获取了记录器信息。 The strange thing is, I get the notice;奇怪的是,我收到了通知;

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

But I don't see the raised validation error in my log files?但是我在日志文件中没有看到引发的验证错误?

Is there anything wrong with my settings?我的设置有问题吗? For some of you this might be piece of cake but I'm struggling to find my own errors, any help will be appreciated!对于你们中的一些人来说,这可能是小菜一碟,但我正在努力寻找自己的错误,任何帮助将不胜感激!

forms.py表格.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")

settings.py设置.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
        },
    }
}

Alright, found an issue within the logging settings;好的,在日志设置中发现了一个问题;

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

This seems to do the trick!这似乎可以解决问题!

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

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