簡體   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