Cannot register custom logging handler class with Django dictConfig

WilHall picture WilHall · Aug 5, 2011 · Viewed 8.8k times · Source

My goal is to create a "log" app, apart from my main app, that will be used for several custom handler classes, filters, etc, and other error and stat reporting. But when running the dev server for my Django project, I receive the error:

  File "/Library/Python/2.7/site-packages/Django-1.3-py2.7.egg/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/Library/Python/2.7/site-packages/Django-1.3-py2.7.egg/django/conf/__init__.py", line 139, in __init__
    logging_config_func(self.LOGGING)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 776, in dictConfig
    dictConfigClass(config).configure()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/config.py", line 575, in configure
    '%r: %s' % (name, e))
ValueError: Unable to configure handler 'db_handler': Unable to configure handler 'db_handler': 'module' object has no attribute 'models'

My LOGGING directive in settings looks like:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'default': {
            'format': '[%(asctime)s] %(levelname)s::(%(process)d %(thread)d)::%(module)s - %(message)s'
        },
    },
    'handlers': {
        'db_handler': {
            'level': 'DEBUG',
            'class': 'Project.log.handlers.db_handler'
        },
        'file_handler': {
            'level': 'DEBUG',
            'formatter':'default',
            'class': 'logging.TimedRotatingFileHandler',
            'filename':'Custom_log',
            'when':'midnight',
            'interval':1
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['db_handler'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}

The layout of my "log" app is pretty simple, and currently:

log/
    __init__.py
    handlers.py
    models.py

models.py contains a single model, LogHandler:

from django.db import models

class LogRecord(models.Model):
    .... 
    ....

and handlers.py contains a single handler:

import logging

from models import LogRecord

class db_handler(logging.Handler):   
    def emit(self, record):
        ....
        ....

The error, as far as I can tell, is referring to the from models import LogRecord line. I have tried using project.log.models, and log.models, but both yield the same results. I have also tried moving the handler into models.py and not importing anything, but I get a similar error saying that "module" has no attribute "models".

My log app is in my installed apps, and contains an __init__.py.

Answer

hoskeri picture hoskeri · Aug 5, 2011

It looks like you are getting a circular import. You cannot define a handler class in a module which itself imports settings.py

It is documented at https://docs.djangoproject.com/en/dev/topics/logging/#topic-logging-parts-handlers (Search for 'circular imports')