[Fixed]-Logging in Django on Heroku not appearing

9👍

Your Procfile is probably at fault here:

If you want to have gunicorn log to stdout you have to use the --logfile=- command line option (you are missing the = !) according to this answer.

So your entire Procfile should look like this:

web: gunicorn myapp.wsgi --log-file=-

EDIT:

Since print statements are working for you, but logging is not, your logging setup is probably at fault. Make sure that you set up logging during the startup of your app (where are you calling dictConfig in your code?):

import logging
logging.config.dictConfig(LOGGING)
logger = logging.getLogger('MYAPP')
logger.info("Just testing")
👤rkrzr

1👍

Your Procfile and LOGGING configuration looks fine. Django configures the logger just before the apps are imported, so if you try to log even before than (for. e.g. from settings.py file), it will not work.

EDIT:

LOGGING config:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': ('%(asctime)s [%(process)d] [%(levelname)s] '
                       'pathname=%(pathname)s lineno=%(lineno)s '
                       'funcname=%(funcName)s %(message)s'),
            'datefmt': '%Y-%m-%d %H:%M:%S'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        }
    },
    'handlers': {
    'null': {
            'level': 'DEBUG',
            'class': 'logging.NullHandler',
    },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
    'ex_logger': {
            'handlers': ['console', ],
            'level': 'INFO',
        }
    }
}

adding following to settings.py won’t log:

import logging
logger = logging.getLogger('ex_logger')
logger.info("core.settings logger")  # won't work 

adding to views.py should log:

from django.http import HttpResponse
import logging

logger = logging.getLogger('ex_logger')
logger.info("core.views logger")  # should work                                                                                                         

def url_please(request):
    logger.info("path: %s" % request.path)  # should work                                                                                               
    return HttpResponse(request.path)
👤Aman

0👍

As stated in previous replies, the logger should be set up in the entry point to your app. In the case of a django based web server using gunicorn, that entry point is most likely the wsgi.py file.
Try adding the required configuration to that file, e.g. setting the global basic logger:

import logging
import os

from dj_static import Cling

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "path.to.settings.py")

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(name)s %(levelname)-8s %(message)s",
)
👤boarik

0👍

I think if you drop a logger.error you would see something.

I had the same problem and it turns out heroku’s settings tool breaks the pre-existing LOGGING setup. The logger you are using is not registered with django but is making its own way to the console using python’s standard logging system.

Try doing:

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

Or better yet, don’t use it at all anymore since this package is no longer maintained anyway.

👤Alper

Leave a comment