Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log all Django form validation errors?

In my Django application, I have a forms.py file in which I define loads of classes corresponding to form input screens. Each of these classes does some validation either in that attribute/field specific _clean() functions or in the overall clean() function for the form class. If validation fails in any of these clean() functions, I raise an error like this:

raise forms.ValidationError('Invalid value dude!!')                

When such an error is raised, the application catches the error and displays it on the form for the user.

I also have a logger defined at the top of this file like this:

import logging
logger = logging.getLogger(__name__)

Now, in addition to reporting to the user the ValidationErrors, I would like to catch them and log them as errors. Is there some generic and elegant way I can get the logger to log all such errors without changing any other behavior that affects the application's user?

like image 746
Saqib Ali Avatar asked Oct 03 '22 00:10

Saqib Ali


1 Answers

You can extend your Forms to log the errors as soon as the validation ends:

class LoggingMixin(object):
    def full_clean(self):
        super(LoggingMixin, self).full_clean()
        for field, errors in self.errors.iteritems():
            logger.info('Form error in %s: %s', ', '.join(errors))

Then define your forms using the logging mixin:

class MyForm(LoggingMixin, forms.Form):
    ...

When Django 1.7 is out, you will also be able to catch the exceptions messages as they are raised. This will give you the raw exceptions as they have been raised (the Form does some more checking):

class LoggingMixin(object):
    def add_error(self, field, error):
        if field:
            logger.info('Form error on field %s: %s', field, error)
        else:
            logger.info('Form error: %s', error)
        super(LoggingMixin, self).add_error(field, error)

WARNING: Using add_error will do nothing in Django <= 1.6.

like image 134
Nicolas Cortot Avatar answered Oct 13 '22 11:10

Nicolas Cortot