简体   繁体   English

详细登录 Django 测试

[英]logging in Django test with verbosity

I'm looking for a way to write to console only when Django tests are run with high verbosity level.我正在寻找一种仅当 Django 测试以高详细级别运行时才写入控制台的方法。 For example - when I run例如 - 当我跑步时

python manage.py test -v 3

It would log my messages to console, but, when I run它会将我的消息记录到控制台,但是,当我运行时

python manage.py test -v 0

It would not log my messages.它不会记录我的消息。

I tried to use logger.info() in the code but the messages do not show up at all.我尝试在代码中使用logger.info()但消息根本不显示。

Any suggestions?有什么建议吗?

-v controls the verbosity of the test runner itself (eg: DB creation, etc), not the verbosity of your app's logging. -v控制测试运行器本身的详细程度(例如:数据库创建等),而不是应用程序日志记录的详细程度。

What you actually want, is to change the logging level of the django app itself, as described in the logging documentation .您真正想要的是更改 django 应用程序本身的日志记录级别,如 日志记录文档中所述

You may want to simply override the logging settings just for tests, or you can use --debug-mode to set settings.DEBUG to True , and include a DEBUG-specific configuration in your settings.py (this is rather common since it also helps when developing).您可能只想覆盖仅用于测试的日志记录设置,或者您可以使用--debug-modesettings.DEBUGTrue ,并在您的 settings.py 中包含特定于调试的配置(这很常见,因为它也开发时有帮助)。

This is a basic example of how to achieve the latter:这是如何实现后者的基本示例:

'loggers': {
    '': {
        'handlers': ['console', 'sentry'],
        'level': 'INFO',
    },
    'myapp': {
        'level': 'DEBUG' if DEBUG else 'INFO',
        'propagate': True,
    },
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM