0
我試圖創建自己的日誌處理程序登錄到數據庫模型,它擴展了logging.Handler自定義Python數據庫記錄器,具有圓形進口
import logging
from logging import Handler
from logger.models import SearchLog
class DBHandler(Handler,object):
model = None
def __init__(self, model):
super(DBHandler, self).__init__()
mod = __import__(model)
components = name.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
self.model = mod
def emit(self,record):
log_entry = self.model(level=record.levelname, message=record.msg)
log_entry.save()
,這是日誌配置:
'db_search_log':{
'level': 'INFO',
'class': 'db_logger.handlers.DBHandler',
'model': 'db_logger.models.SearchLog',
'formatter': 'verbose',
}
但是我收到後續錯誤,請參閱堆棧跟蹤:
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 252, in fetch_command
app_name = get_commands()[subcommand]
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 101, in get_commands
apps = settings.INSTALLED_APPS
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/home/james/virtualenv/hydrogen/local/lib/python2.7/site-packages/django/conf/__init__.py", line 135, in __init__
logging_config_func(self.LOGGING)
File "/usr/lib/python2.7/logging/config.py", line 777, in dictConfig
dictConfigClass(config).configure()
File "/usr/lib/python2.7/logging/config.py", line 575, in configure
'%r: %s' % (name, e))
ValueError: Unable to configure handler 'db_search_log': Unable to configure handler 'db_search_log': 'module' object has no attribute 'handlers'
▾
db_logger/
__init__.py
__init__.pyc
handlers.py
handlers.pyc
log_handlers.pyc
models.py
models.pyc
router.py
router.pyc
tests.py
views.py
感謝@istruble指出,這是由於循環導入設置,我該如何避免它並仍然登錄到數據庫模型?
你是怎麼運行的?你能用'python -m pdb'啓動它,或者可能把'import pdb;在您的settings.py中設置pdb.set_trace()'並繼續,以便在出現錯誤時可以獲得pdb提示並檢查狀態? –
可能是settings.py的循環導入?從文檔的這一部分向上滾動6行https://docs.djangoproject.com/en/dev/topics/logging/#custom-logging-configuration – istruble
@istruble是它是由於循環導入,因爲日誌處理程序使用模型... –