roob
roob

Reputation: 2549

Django logging does not work with django shell

I have logging set up in my Django app to log to both stdout and a file.

When I run my django app normally (./manage.py runserver) the logs are updated as expected. Here is an example that successfully logs when i visit the url for the view below:

# views.py

import logging
logger = logging.getLogger('mylogger')

def test(request):
    logger.error("test")

However, when I run ./manage.py shell and run the following commands the log is not updated:

import logging
logger = logging.getLogger('mylogger')
logger.error('test')

Here is my logging setup in settings.py:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'stream': sys.stdout,
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '../debug.log',
        },
    },
    'loggers': {
        'mylogger': {
            'handlers': ['console', 'file'],
            'level': 'INFO',
            'propagate': True,
        },
    }
}

Do I have to do something extra to get logging to work within the shell?

Upvotes: 6

Views: 4913

Answers (2)

Gul Shair Butt
Gul Shair Butt

Reputation: 149

during working with django-shell in Pycharm i was facing the same issue. There are two ways to solve it.

  1. When you django-shell is started go and import it manually:

    import logging; l=logging.getLogger('django.db.backends');l.setLevel(logging.DEBUG);l.addHandler(logging.StreamHandler())

  2. Or if you are using the Pycharm professional edition like me than you can add this import on console startup you can see in https://www.jetbrains.com/help/pycharm/console-django-console.html official docs. To do this, go to your File | Settings | Build, Execution, Deployment | Console | Django Console for Windows and Linux

PyCharm | Preferences | Build, Execution, Deployment | Console | Django Console for macOS

and add this import command on console startup.

Upvotes: 2

Vinay Sajip
Vinay Sajip

Reputation: 99495

It should work fine in the shell. It does for me, with the same logging configuration as you (except that I used 'ext:://sys.stdout' instead of sys.stdout, as I didn't have sys imported in settings.py.

django20 vinay@ubuntu:/tmp/foo$ tail -25 foo/settings.py 
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
    },
    'handlers': {
        'console': {
            'level': 'INFO',
            'class': 'logging.StreamHandler',
            'stream': 'ext://sys.stdout',
        },
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '../debug.log',
        },
    },
    'loggers': {
        'mylogger': {
            'handlers': ['console', 'file'],
            'level': 'INFO',
            'propagate': True,
        },
    }
}

django20 vinay@ubuntu:/tmp/foo$ python manage.py shell
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import logging; logger = logging.getLogger('mylogger')
>>> logger.error('foo')
foo
>>> logger.warning('bar')
bar
>>> logger.info('baz')
baz
>>> logger.debug('boz')
>>> 

django20 vinay@ubuntu:/tmp/foo$ more ../debug.log 
foo
bar
baz

This was using a fresh virtualenv with a Django 2.0 installation, but it should work exactly the same with older versions of Django.

Upvotes: 3

Related Questions