Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 607
Eliav Gnessin Avatar asked Oct 30 '22 02:10

Eliav Gnessin


1 Answers

-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,
    },
}
like image 127
WhyNotHugo Avatar answered Nov 09 '22 22:11

WhyNotHugo