Eliav Gnessin
Eliav Gnessin

Reputation: 103

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?

Upvotes: 8

Views: 6198

Answers (1)

WhyNotHugo
WhyNotHugo

Reputation: 9924

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

Upvotes: 2

Related Questions