Search code examples
pythonpython-3.xdjangologgingdjango-views

Django logging isn't working, despite having proper settings


Problem and background

Hi, I have a Django project using Django 2.2.28, and python 3.5.2.

The live site runs fine, except for one function I'm trying to debug.

I've used logging before, but for some reason it's not working now.

Here's my code...

production_settings.py

I call this file in wsgi.py. It's settings all work as they should, but LOGGING isn't.

from .settings import *

DEBUG = False

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': '/home/myuser/proj_container/debug.log',
        },
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': False,
        },
    },
}

views.py

import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)

def some_func(request):
    ...
    if some_condition:
        logger.debug('In some_condition block')
    elif some_other_condition:
        logger.debug('In some_other_condition block')

What I've tried

  1. I've tried the above with just the settings in production_settings.py... Nothing shows up.

  2. Same as 1, but with logging imported and called at every important code block.

  3. I read the docs for logger.debug(), as well as this answer that I implemented above as you can see.

  4. I also reread the Django logging docs.

  5. I moved LOGGING from production_settings.py to settings.py... Yesterday I fixed a bug where django wasn't overriding the DATABASES variable in settings.py from production_settings.py, and was instead making a sqlite3 database and giving errors from settings.py, and the only way it got fixed was by doing this.

  6. For every change I've made above: I've always restarted the wsgi and the website (the bound unix socket service, because there is more than 1 site running on this server, and both sites work fine except for this problem on one of them). The cached values for them are not conflicting, my file paths are correct, etc.

Question

Why isn't this working? What am I missing?

This has worked before just fine on other projects, but I have no idea why it's not now.

Thanks!


Solution

  • Here's a work around. I tested and tested my code above again, and I see nothing wrong with it, even re-read the entire relative doc pages again, so it's probably just a bug on that version.

    Anyways, here's a solution for anyone who comes across this problem too.

    Just make sure that your Linux user has permissions to write to the file path below.

    Views.py

    import logging
    
    logging.basicConfig(level=logging.DEBUG, filename='/home/myuser/path/to/debug.log')  # will append by default, docs allow other values too.
    logger = logging.getLogger(__name__)
    
    def some_func(request):
        ...
        if some_condition:
            logger.debug('In some_condition block')
            logger.debug('Object type == {}'.format(type(some_condition.attribute['key']['nested_key'])))
            logger.debug('\n\n')
            # etc
        elif some_other_condition:
            logger.debug('In some_other_condition block')
    

    settings.py or production_settings.py

    # LOGGING = {...} # is not necessary since the above does it fine. Probably just a bug like I mentioned with DATABASES.