设置日志记录时,我遇到了一个奇怪的行为。这是我的 记录配置:
LOGGING =
{
'version': 1,
'disable_existing_loggers': True,
'formatters': {
'admin_configuration': {
'format': '%(asctime)s %(levelname)s %(category)s % (sub_category)s %(type_id)s %(message)s',
'datefmt': LOG_DATE_FORMAT,
},
},
'handlers': {
'admin_console': {
'level':'DEBUG',
'class':'logging.StreamHandler',
'formatter': 'admin_configuration'
},
'db': {
'level':'DEBUG',
'class':'apps.history.handlers.DatabaseHandler',
'formatter': 'admin_configuration'
},
},
'loggers': {
'db_logger': {
'handlers': [ 'admin_console', 'db' ],
'level': 'DEBUG',
'propagate': False,
},
}
}
然后我创建了一个数据库日志处理程序,日志记录将是 使用信号创建(例如在模型保存/删除),因为我正在尝试 跟踪模型的变化。正如你从我的配置中看到的那样 logger有2个处理程序,1个用于DB,1个用于stdout(流处理程序)。
我以这种方式使用记录器:
logging.getLogger('db_logger').log(level, msg, extra=extras)
其中extras是我传递给记录器的额外属性。
现在奇怪的是,当我改变'处理程序'的顺序时
在'db_logger'下定义到['db', 'admin_console']
,记录器
在我的DatabaseHandler中引发此错误(参见下面的定义):
LogRecord instance has no attribute 'asctime'
检查记录实例后,未生成“message”属性
要么,但我的所有额外属性都存在。
如果我删除'admin_console'处理程序,则会发生同样的错误。唯一的
我可以让记录器工作的方式是处理程序必须在
原始订单:'处理程序':[ 'admin_console', 'db' ]
回溯:
File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/
base.py" in get_response
89. response = middleware_method(request)
File "/home/user/projects/django/../django/common/middleware.py" in
process_request
27. return login(request, **defaults)
File "/usr/local/lib/python2.6/dist-packages/django/utils/
decorators.py" in _wrapped_view
93. response = view_func(request, *args,
**kwargs)
File "/usr/local/lib/python2.6/dist-packages/django/views/decorators/
cache.py" in _wrapped_view_func
79. response = view_func(request, *args, **kwargs)
File "/home/user/projects/django/../django/apps/django_extra_auth/
views.py" in login
57. auth_login(request, form.get_user())
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/
__init__.py" in login
85. user_logged_in.send(sender=user.__class__, request=request,
user=user)
File "/usr/local/lib/python2.6/dist-packages/django/dispatch/
dispatcher.py" in send
172. response = receiver(signal=self, sender=sender,
**named)
File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/
models.py" in update_last_login
50. user.save()
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py"
in save
460. self.save_base(using=using, force_insert=force_insert,
force_update=force_update)
File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py"
in save_base
570. created=(not record_exists), raw=raw,
using=using)
File "/usr/local/lib/python2.6/dist-packages/django/dispatch/
dispatcher.py" in send
172. response = receiver(signal=self, sender=sender,
**named)
File "/home/user/projects/django/apps/history/audit.py" in
post_save_handler
102. logging.log_to_db(logging.INFO, msg, log_type)
File "/home/user/projects/django/apps/history/__init__.py" in
log_to_db
61. db_logger.log(level, msg, extra=extras)
File "/usr/lib/python2.6/logging/__init__.py" in log
1119. self._log(level, msg, args, **kwargs)
File "/usr/lib/python2.6/logging/__init__.py" in _log
1173. self.handle(record)
File "/usr/lib/python2.6/logging/__init__.py" in handle
1183. self.callHandlers(record)
File "/usr/lib/python2.6/logging/__init__.py" in callHandlers
1220. hdlr.handle(record)
File "/usr/lib/python2.6/logging/__init__.py" in handle
679. self.emit(record)
File "/home/user/projects/django/apps/history/handlers.py" in emit
11. timestamp = datetime.strptime(record.asctime,
settings.LOG_DATE_FORMAT)
数据库记录器包装
def log_to_db(self, level, msg, log_type, extra={}):
db_logger = logging.getLogger('db_logger')
extras = {
'category': log_type.category.name,
'sub_category': log_type.sub_category.name,
'type_id': log_type.type_id,
}
extras.update(extra)
db_logger.log(level, msg, extra=extras)
数据库处理程序
class DatabaseHandler(logging.Handler):
def emit(self, record):
user = getattr(record, 'user', None)
category = getattr(record, 'category', None)
sub_category = getattr(record, 'sub_category', None)
type_id = getattr(record, 'type_id', None)
timestamp = datetime.strptime(record.asctime, settings.LOG_DATE_FORMAT)
from history.models import LogRecord
LogRecord.objects.create(timestamp=timestamp, level=record.levelname,
category=category, sub_category=sub_category, type_id=type_id,
message=record.message, user=user)
知道为什么会这样吗?
答案 0 :(得分:1)
您必须在self.format(record)
中致电DatabaseHandler.emit()
,格式化工具会为您设置这些变量。
您可能想要查看此应用:https://github.com/zain/jogging