我正在尝试调试一个有问题的错误,该错误在我尝试启动我正在使用(以及其他)celery 和 rollbar 的 django 项目时发生。当我将日志级别更改为mail_admins
from ERROR
to时,会出现问题WARNING
。完整的追溯如下:
(env)root@test:/var/www/papukurier_test# ./manage.py migrate
2018-01-30 13:30:52 [16517] [WARNING] pathname=/var/www/papukurier_test/env/local/lib/python2.7/site-packages/rollbar/__init__.py lineno=292 funcname=init Rollbar already initialized. Ignoring re-init.
Traceback (most recent call last):
File "./manage.py", line 13, in <module>
execute_from_command_line(sys.argv)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute
django.setup()
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/apps/registry.py", line 85, in populate
app_config = AppConfig.create(entry)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/apps/config.py", line 120, in create
mod = import_module(mod_path)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/var/www/papukurier_test/courier/apps.py", line 4, in <module>
from courier.api.consumers import INIT_COURIERS_CONSUMERS_SIGNATURE
File "/var/www/papukurier_test/courier/api/consumers.py", line 10, in <module>
from papukurier.celery import celery_app
File "/var/www/papukurier_test/papukurier/celery.py", line 23, in <module>
rollbar.init(**settings.ROLLBAR)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/rollbar/__init__.py", line 292, in init
log.warning('Rollbar already initialized. Ignoring re-init.')
File "/usr/lib/python2.7/logging/__init__.py", line 1164, in warning
self._log(WARNING, msg, args, **kwargs)
File "/usr/lib/python2.7/logging/__init__.py", line 1271, in _log
self.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1281, in handle
self.callHandlers(record)
File "/usr/lib/python2.7/logging/__init__.py", line 1321, in callHandlers
hdlr.handle(record)
File "/usr/lib/python2.7/logging/__init__.py", line 749, in handle
self.emit(record)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/log.py", line 119, in emit
message = "%s\n\n%s" % (self.format(no_exc_record), reporter.get_traceback_text())
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/views/debug.py", line 334, in get_traceback_text
return t.render(c)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/template/base.py", line 1040, in render
output = self.filter_expression.resolve(context)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/template/base.py", line 736, in resolve
new_obj = func(obj, *arg_vals)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/template/defaultfilters.py", line 764, in date
return formats.date_format(value, arg)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/formats.py", line 162, in date_format
return dateformat.format(value, get_format(format or 'DATE_FORMAT', use_l10n=use_l10n))
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 371, in format
return df.format(format_string)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 42, in format
pieces.append(force_text(getattr(self, piece)()))
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 297, in r
return self.format('D, j M Y H:i:s O')
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/dateformat.py", line 42, in format
pieces.append(force_text(getattr(self, piece)()))
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/encoding.py", line 78, in force_text
s = six.text_type(s)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 119, in __text_cast
return func(*self.__args, **self.__kw)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 89, in ugettext
return _trans.ugettext(message)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 345, in ugettext
return do_translate(message, 'ugettext')
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 321, in do_translate
_default = _default or translation(settings.LANGUAGE_CODE)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 228, in translation
_translations[language] = DjangoTranslation(language)
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 129, in __init__
self._add_installed_apps_translations()
File "/var/www/papukurier_test/env/local/lib/python2.7/site-packages/django/utils/translation/trans_real.py", line 171, in _add_installed_apps_translations
"The translation infrastructure cannot be initialized before the "
django.core.exceptions.AppRegistryNotReady: The translation infrastructure cannot be initialized before the apps registry is ready. Check that you don't make non-lazy gettext calls at import time.
(env)root@test:/var/www/papukurier_test#
这是部分日志记录设置:
'handlers': {
'console': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
'mail_admins': {
'level': 'INFO',
'class': 'django.utils.log.AdminEmailHandler',
'formatter': 'verbose'
},
'mail_admins_command': {
'level': 'DEBUG',
'class': 'django.utils.log.AdminEmailHandler',
'formatter': 'verbose'
},
'console_command': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'plain'
},
'rollbar': {
'level': 'ERROR',
'class': 'rollbar.logger.RollbarHandler'
},
},
'loggers': {
'': {
'handlers': ['console', 'mail_admins', 'rollbar'],
'level': 'DEBUG',
'propagate': False,
},
'django': {
'handlers': ['console', 'mail_admins', 'rollbar'],
'level': 'INFO',
'propagate': False,
},
'papu': {
'handlers': ['console', 'mail_admins', 'rollbar'],
'level': 'DEBUG',
'propagate': False,
},
'invoices.management.commands': {
'handlers': ['console_command', 'mail_admins_command', 'rollbar'],
'level': 'DEBUG',
'propagate': False,
},
'xhtml2pdf': {
'handlers': ['console_command', 'mail_admins_command'],
'level': 'ERROR',
'propagate': False,
},
'django.security.DisallowedHost': {
'handlers': ['console', 'mail_admins', 'rollbar'],
'level': 'CRITICAL',
'propagate': False,
}
}
}
到目前为止,我已经完成了这个讨论,但没有一个建议的解决方案适用于我的案例。我开始怀疑日志记录是否存在问题,因为如果您查看回溯,会发现 rollbar reinit 警告的日志记录不是由环境 Python(位于env/
目录中)而是由系统记录器处理的。
当我尝试运行 python 控制台时,我得到了这个:
(env)root@test:/var/www/papukurier_test# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import logging
>>> logging
<module 'logging' from '/usr/lib/python2.7/logging/__init__.pyc'>
编辑
我的环境中没有loggin
模块。
(env)root@test:/var/www/papukurier_test# ls env/lib/python2.7/ | grep logging
(env)root@test:/var/www/papukurier_test#