8

I am trying to set up a custom log filter in django, but I am stuck with an error:

ValueError: Unable to configure filter 'user_filter': 'module' object has no attribute 'Filter'

I get this error when I try to run the django development server.

The purpose of this is to log name of the user, which is a request variable defined in my middleware.

Here is what I have in my settigs.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s %(user)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'filters': {
        'user_filter': {
            '()': 'creative.logging.UserFilter',
        }
    },
    'handlers': {
        'default': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            'filename': BASE_DIR+'/logs/default.log',
            'maxBytes': 1024*1024*5, # 5 MB
            'backupCount': 5,
            'formatter':'standard',
        },
        'request_handler': {
                'level':'DEBUG',
                'class':'logging.handlers.RotatingFileHandler',
                'filename': BASE_DIR+'/logs/django_request.log',
                'maxBytes': 1024*1024*5, # 5 MB
                'backupCount': 5,
                'formatter':'standard',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'format':'%(asctime)s | %(levelname)s | %(funcName)s |%(message)s',
            'propagate': False,
            'filters': ['user_filter']
        },
        'django.request': {
            'handlers': ['request_handler'],
            'level': 'DEBUG',
            'propagate': False
        },
    }
}

and in creative/logging.py:

import logging

class UserFilter(logging.Filter):
     def filter(self, record):
         record.user = record.request.user_profie['fullName']
         return True

Any ideas?

4
  • 3
    Just do not name the module logging.py. Commented May 23, 2014 at 14:59
  • renamed it to customlogging, and changed in settings.py, I still get the same error Commented May 23, 2014 at 15:02
  • 3
    Make sure logging.pyc is not there. Commented May 23, 2014 at 15:03
  • 2
    Hi, I am trying to use the similar code by adding it in django settings file. But I am getting this error AttributeError: 'LogRecord' object has no attribute 'request' . Any guess what I am doing wrong? Thanks Commented Nov 22, 2018 at 8:01

1 Answer 1

11

As said in the comments, you named your file logging.py so it overrides the default module chosen by import logging.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.