Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Logging with FileHandler not Working

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,
        },
    },
}
like image 325
moinudin Avatar asked Jun 09 '12 02:06

moinudin


1 Answers

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.

like image 138
Jeremy Stretch Avatar answered Sep 25 '22 15:09

Jeremy Stretch