8

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?

1 Answer 1

2

-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,
    },
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.