gpt4 book ai didi

python - 如何禁用 django 迁移调试日志记录?

转载 作者:太空狗 更新时间:2023-10-30 01:00:47 24 4
gpt4 key购买 nike

非常类似于lafagundes question about south migration debug logging ,除了我没有使用 south - 我使用的是普通的 Django 1.7 迁移。我也在使用 django-nose测试运行器。

当我运行 manage.py test 时,没有捕获调试日志输出:

(codesy)lcrouch:codesy lcrouch$ ./manage.py test
nosetests --verbosity=1
Creating test database for alias 'default'...
......E...............................
======================================================================
ERROR: test_return_state (auctions.tests.utils_tests.IssueStateTest)
----------------------------------------------------------------------

当我运行单个测试模块时,例如 ./manage.py test auctions.tests.utils_tests,调试日志输出包括所有 django.db.backends.schema : DEBUG 行涉及 Django 迁移:

(codesy)lcrouch:codesy lcrouch$ ./manage.py test auctions.tests.utils_tests
nosetests auctions.tests.utils_tests --verbosity=1
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_return_state (auctions.tests.utils_tests.IssueStateTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/lcrouch/code/codesy/codesy/auctions/tests/utils_tests.py", line 13, in test_return_state
fake_gh_client = fudge.Fake(github_client).returns_fake().provides('get_repo').returns_fake().provides('get_issue').returns_fake().has_attr(state='open')
File "/Users/lcrouch/python/codesy/lib/python2.7/site-packages/fudge/__init__.py", line 1133, in returns_fake
exp = self._get_current_call()
File "/Users/lcrouch/python/codesy/lib/python2.7/site-packages/fudge/__init__.py", line 765, in _get_current_call
"Call to a method that expects a predefined call but no such call exists. "
FakeDeclarationError: Call to a method that expects a predefined call but no such call exists. Maybe you forgot expects('method') or provides('method') ?
-------------------- >> begin captured logging << --------------------
django.db.backends.schema: DEBUG: CREATE TABLE "django_migrations" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "app" varchar(255) NOT NULL, "name" varchar(255) NOT NULL, "applied" datetime NOT NULL); (params [])
django.db.backends.schema: DEBUG: CREATE TABLE "django_content_type" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL, "app_label" varchar(100) NOT NULL, "model" varchar(100) NOT NULL); (params [])
django.db.backends.schema: DEBUG: CREATE TABLE "django_content_type__new" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(100) NOT NULL, "app_label" varchar(100) NOT NULL, "model" varchar(100) NOT NULL, UNIQUE ("app_label", "model")); (params [])
...
django.db.backends.schema: DEBUG: DROP TABLE "socialaccount_socialapp"; (params [])
django.db.backends.schema: DEBUG: ALTER TABLE "socialaccount_socialapp__new" RENAME TO "socialaccount_socialapp"; (params [])

这真的很难回到真正的失败。

如何禁用此输出?在 django 或 Nose 水平?

最佳答案

如果您不介意在测试中禁用迁移,您可以通过将以下内容添加到您的 settings.py 中来实现:

TESTING = 'test' in sys.argv

if TESTING:
class DisableMigrations(object):
def __contains__(self, item):
return True

def __getitem__(self, item):
return "notmigrations"

MIGRATION_MODULES = DisableMigrations()

这只会一次性创建数据库,而不是逐步完成每个迁移。

如果您想在测试中保持迁移运行,您可以通过更新日志记录设置来删除消息。

TESTING = 'test' in sys.argv

if TESTING:
LOGGING = {
'version': 1,
'handlers': {
'file': {
'level': 'DEBUG',
'class': 'logging.FileHandler',
'filename': '/tmp/codesy-debug.log',
},
},
'loggers': {
'django.db.backends.schema': {
'handlers': ['file'],
'propagate': True,
'level': 'INFO',
},
'': {
'handlers': ['file'],
'level': 'DEBUG',
}
}
}

关于python - 如何禁用 django 迁移调试日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32158773/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com