简体   繁体   中英

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. 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.

Any suggestions?

-v controls the verbosity of the test runner itself (eg: DB creation, etc), not the verbosity of your app's logging.

What you actually want, is to change the logging level of the django app itself, as described in the logging documentation .

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).

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,
    },
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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