How to stop writing to Django unittests from print to stderr?

I am testing some Django models with bog-standerd django.test.Testcase. My models.py files are written to the debug log using the following initialization code:

import logging
logger = logging.getLogger(__name__) # name is myapp.models

and then I write to the log with:

logger.debug("Here is my message")

In my settings.py, I installed one FileHandler and a log for myapp using this handler and only this handler. Fine. I see posts in this journal. When I am in the Django shell, I only see messages in this log.

When, however, I run my test suite, the test suite console also sees all of these messages. It uses a different formatter that I have not explicitly defined, and it writes stderr. I do not have a log handler that writes to stderr.

I do not want these messages spamming my console. I will close my log file if I want to see these messages. Is there any way to stop it? (Yes, I could redirect stderr, but useful output goes to stderr as well.)

Edit: I installed two handlers in my settings.py:

'handlers': {
    'null': {
        'level': 'DEBUG',
        'class': 'django.utils.log.NullHandler',
    },
    'logfile' : {
        'level':'DEBUG',
        'class':'logging.FileHandler',
        'filename':'%s/log/development.log' % PROJECT_DIR,
        'formatter': 'simple'
    },
},

and tried this:

'loggers': {
    'django': {
        'level': 'DEBUG',
        'handlers': ['null']
    },
    'myapp': {
        'handlers': ['logfile'],
        'level':'DEBUG',
    },

... but the logging / stderr dumping behavior remains unchanged. This is similar to the fact that I get another log handler when I run the tests.

+5
source share
1 answer

, , , . ( , Django 1.3.) , ? AFAICT Django - , - , , basicConfig, . - ack-grep, fileConfig, dictConfig, basicConfig addHandler - .

, : False (, "django", , , - , "myapp" ). - ?

+3
source

All Articles