Logging in Django and gunicorn

Pavel Ryvintsev picture Pavel Ryvintsev · Dec 29, 2014 · Viewed 9k times · Source

I'm running django application with gunicorn, and I can't see any log messages I'm wriging.

Here is the code that writes the logs:

logger = logging.getLogger(__name__)

def home_page(request):
    logger.warning('in home page')

(note: this code definitely runs, since this is a view that leads to the homepage)

This is my logs configuration from settings.py:

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

I run gunicorn as daemon with the following arguments:

--access-logfile ../access.log --error-logfile --log-level debug ../error.log

Both access.log and error.log are created and filled with gunicorn messages, but I can't see the messages I write.

Thanks

Answer

Pavel Ryvintsev picture Pavel Ryvintsev · Dec 30, 2014

I have solved my problem. Providing the details so it might help somebody with similar issue.

Decided not to mix up with gunicorn and django logs and to create separate log file for django.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
        'logfile': {
            'level':'DEBUG',
            'class':'logging.FileHandler',
            'filename': BASE_DIR + "/../logfile",
        },
    },
    'root': {
        'level': 'INFO',
        'handlers': ['console', 'logfile']
    },
}

With this configuration every message written with severity >= INFO will be written to file "logfile" located just outside of source directory.