2

I'm having difficulty getting the desired behavior at the intersection of the django test runner, i.e. from django.test import TestCase and the logging solution.

my logger is set up and works in other areas with something like this

import logging
logger = logging.getLogger(__name__)
logger.warning("duck!")

so when running the Django app, I see the message and metadata as I expect; however, the trouble is that these log lines do not show up in any location I am aware of when running the unit tests, i.e. django-admin run test

I want to be able to run tests and see the log output, ideally in STDOUT, and presumably set the output log levels so that tests show those...

my settings vis. logging:

   LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'root': {
        'level': 'ERROR',
        'handlers': ['sentry', 'console'],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s '
                      '%(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'sentry': {
            'level': 'WARNING', # To capture more than ERROR, change to WARNING, INFO, etc.
            'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler',
            'tags': {'custom-tag': 'x'},
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console'],
            'propagate': False,
        },
        'raven': {
            'level': 'WARNING',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'celery': {
            'handlers': ['console'],
            'level': 'WARNING',
            'proagate': True,
        },
    },
}
Ben
  • 4,980
  • 3
  • 43
  • 84
  • 2
    looks like this: https://stackoverflow.com/questions/1236285/how-do-i-see-stdout-when-running-django-tests – Jonah Jul 16 '18 at 00:36
  • it is related, but none of the answers get very close to the issue. For one, I am not using Nose. Perhaps more to the point, my issue is that `print` or `pprint` statements do get printed to STDOUT just fine. What I don't know how to do is cause the `logger.loglevel(msg)` statements to also be printed there – Ben Jul 16 '18 at 15:35

1 Answers1

0

Try changing your root level to WARNING:

    'root': {
        'level': 'WARNING',
        'handlers': ['sentry', 'console'],
    },
Ben Wheeler
  • 6,788
  • 2
  • 45
  • 55