简体   繁体   中英

Logging not showing in console during django tests

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM