0
votes

I have been following the Django Logging docs but haven't been able to produce logs. All of this is within a virtualenv.

My views.py file contains

import logging
logger = logging.getLogger(__name__)

def index(request):
    logger.debug('Index page')
    return render(request, 'app/index.html')

In settings.py I have:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/pathtologfile/debug.log',
        },
    },
    'loggers': {
        '': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

No logs are appearing in the log file from views.py when I navigate to the index page.

However, if I use the django shell to execute the following commands the log will show up in the log file:

>>>import logging
>>>logger = logging.getLogger(__name__)
>>>logger.debug('shell')

How can I get logs from view.py (or any file for that matter)?

I have tried the following with no result:

Is there anything wrong with my settings? Any where to even start looking at to debug this?

1

1 Answers

0
votes

Even I was facing the same issue! Just use the following settings:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'standard': {
            'format': '%(asctime)s [%(levelname)s] %(name)s: %(message)s'
        },
    },
    'handlers': {
        'default': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'debug.log',
        },
    },
    'loggers': {
        '': {
            'handlers': ['default'],
            'level': 'DEBUG',
            'propagate': True,
        },
    },
}

debug.log file will be created in the root folder of the django project(where manage.py resides)