自定义python数据库记录器,具有循环导入 [英] Custom python database logger, having circular import
本文介绍了自定义python数据库记录器,具有循环导入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建自己的日志处理程序以登录到数据库模型,从而扩展了logging.Handler
I am trying to create my own log handler to log to db models, which extends logging.Handler
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',
}
但是我遇到了以下错误,请参见stacktrace:
however I am getting the follow error, see stacktrace:
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指出这是由于循环导入设置,如何避免这种情况并仍然登录到数据库模型?
Thanks to @istruble pointed out that that is due to circular imports of settings, how can I avoid it and still log to the database models?
推荐答案
我刚刚想出了另一种使用延迟导入来实现它的更规范的方法,我最初的问题是试图在 init 中导入模型strong>功能:
I just came up with another actually more canonical way of implementing it using delayed imports, my original problem was trying to import the model inside init function:
from logging import Handler
class DBHandler(Handler,object):
model_name = None
def __init__(self, model=""):
super(DBHandler,self).__init__()
self.model_name = model
def emit(self,record):
# instantiate the model
try:
model = self.get_model(self.model_name)
except:
from logger.models import GeneralLog as model
log_entry = model(level=record.levelname, message=self.format(record))
log_entry.save()
def get_model(self, name):
names = name.split('.')
mod = __import__('.'.join(names[:-1]), fromlist=names[-1:])
return getattr(mod, names[-1])
这篇关于自定义python数据库记录器,具有循环导入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文