5

I am trying some simple tests in django. I have setup my django logging system in my settings file. But when I run my test it won't print debug messages on my console. This happens when I run tests from manage.py test command. If I use my IDE's run command It prints the messages normally. My logging setup is the following

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d (thread)d %(message)s'
        },
        'simple':{
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'null': {
            'level': 'DEBUG',
            'class': 'django.utils.log.NullHandler',
        },
        'console': {
            'level':'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'simple',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console', ],
            'propagate': True,
            'level': 'DEBUG'

        },
        'payments_system': {
            'handlers': ['console', ],
            'propagate': True,
            'level': 'DEBUG'
        }
    }
}

Logging is based on django's website example. How can I make messages appear in the console when I run tests with manage.py?

Apostolos
  • 7,763
  • 17
  • 80
  • 150
  • 2
    May see here https://stackoverflow.com/questions/1236285/how-do-i-see-stdout-when-running-django-tests, django's testrunner seems to capture stdout, so your logging messages might get swallowed. – dhke May 30 '15 at 19:52

1 Answers1

0

I have Django logging set up to log to a file, like this:

'handlers': {
    'file': {
        'level': 'DEBUG',
        'class': 'logging.FileHandler',
        'filename': '/path/to/django/debug.log',
    },
},

When I run tests with python manage.py test, the logs also go to that file.

Since you're using a console handler, your logs are should be going to stdout, which is likely being captured filtered by the test code. So try using a FileHandler instead. Refer to the first example under Configuring Logging for more details.

Michael Scheper
  • 6,514
  • 7
  • 63
  • 76