Not seeing Django logs on Heroku

I'm not seeing log entries (at a level of INFO) made by Django in my Heroku logs.

This is my configuration:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse',
        },
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
        'not_development_filter': {
            '()': NotDevelopmentFilter,
        },
    },
    'handlers': {
        'console':{
            'level': 'INFO',
            'class': 'logging.StreamHandler',
        },
        'null': {
            'class': 'django.utils.log.NullHandler',
        },
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['not_development_filter'],
            'class': 'django.utils.log.AdminEmailHandler',
            'include_html': True,
        }
    },
    'loggers': {
        '': {
            'handlers': ['mail_admins', 'console'],
            'level': 'INFO',
        },
        'django': {
            'handlers': ['console'],
        },
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': False,
        },
        'py.warnings': {
            'handlers': ['console'],
        },
    }
}

I'd like to see the log entries in the Heroku interface. Any idea why I'm not seeing them there?


Solution 1:

In the python-getting-started app, for a log that isn't tagged django, ERROR level logs show up in heroku logs, but INFO logs don't.

To make it work for logs that aren't tagged django, it needs a config like the following (similar to yours):

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        '': {
            'handlers': ['console'],
            'level': 'INFO',
        },
        'django': {
            'handlers': ['console'],
            'level': 'INFO',
        },
    },
}

So if you adjust your configuration to otherwise be like python-getting-started, including the Procfile, it should hopefully work.

Here's a log that isn't tagged django, that I added to views/hello.py:

import logging
logger = logging.getLogger(__name__)

# Create your views here.
def index(request):
    logger.error('testing logging!')
    logger.info('testing info logging')
    logger.debug('testing debug logging')

    # return HttpResponse('Hello from Python!')
    return render(request, 'index.html')

When I first tried it, only the error log showed up. When I added the logging config in the code snippet above the previous one, the info and the error logs showed up. This is all with DEBUG = False (changed from the code in the repo, which has DEBUG = True).

It probably is better to, rather than have the empty string key, so everything is logged, to have a more specific loggers entries than just the empty string ('').

Finally, in your log file, there's a case where INFO logs won't appear on the console, or Heroku logs: If it's an INFO log to django.request, with your config it will only go to 'mail_admins' because propagate is False. I think that in this case, it would make more sense for propagate to be set to True.

Solution 2:

I had the same thing that INFO level does not show up but in my case (and as @Benjamin Atkin reports as well) ERROR did.

The core problem seems to be that this call:

django_heroku.settings(locals())

removes whatever custom loggers you might have setup in your LOGGING dict.

The solution is to add this to the call so that it does not mess with your logging setup:

django_heroku.settings(locals(), logging=False)

Or better yet don't use it at all, since this package is deprecated anyway.