Django Logging with FileHandler not Working

moinudin picture moinudin · Jun 9, 2012 · Viewed 7.1k times · Source

I am using the logging setup below with a django project (also using sentry/raven). The sentry/raven bit is working fine, but the file logging isn't. An empty logfile is created, but whenever I use logging.info('foo') nothing comes up in the log file (i.e. it remains empty). Any suggestions?

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'root': {
        'level': 'WARNING',
        'handlers': ['sentry'],
    },
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'sentry': {
            'level': 'ERROR',
            'class': 'raven.contrib.django.handlers.SentryHandler',
        },
        'file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': '/var/log/django/breeding.log',
        },
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'ERROR',
            'handlers': ['console'],
            'propagate': False,
        },
        'raven': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
        'sentry.errors': {
            'level': 'DEBUG',
            'handlers': ['console'],
            'propagate': False,
        },
    },
}

Answer

Jeremy Stretch picture Jeremy Stretch · Mar 23, 2014

I ran into this same issue. It turned out to be a permissions problem. When I ran the development server for the first time after configuring logging, it created the file /var/log/django/request.log owned by my local user (stretch) with mode 644.

When I started the "production" server (nginx/uwsgi), the service would run as the www-data user and was unable to open /var/log/django/request.log for writing. Simply deleting the log file and restarting uwsgi was enough to get it going, but I'll have to come up with a more elegant long-term fix.