Django Logging Does Not Work When Deploying on Ubuntu / Apache Server

advertisements

My Django Logger works fine on my local machine but does not work when I deploy it to the server.

  • Local: OS X 10.9.5, Python 2.7.5, Django 1.6.2
  • Server: Ubuntu 12.04, Apache 2.2.22, mod_wsgi Version: 3.3-4ubuntu0.1, Python 2.7.3, Django 1.6

On my local setup I run python manage.py syncdb, this creates the survey.log file which I then follow with tail -f survey.log so I can see the error messages as they are created.

On my server I run python manage.py syncdb, this creates the survey.log file which I then follow with tail -f survey.log. However I can not see any of my debug messages and when I inspect the file with nano it is empty.

Why is no logging data being recorded into survey.log on my production environment? What am I missing?

views.py

import logging
logger = logging.getLogger(__name__)

logger.debug('This is your images list in 7: %s', images)

settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        },
        'applogfile': {
            'level':'DEBUG',
            'class':'logging.handlers.RotatingFileHandler',
            #'filename': os.path.join(DJANGO_ROOT, 'survey.log'),
            'filename': 'survey.log',

            'maxBytes': 1024*1024*15, # 15MB
            'backupCount': 10,
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
        'survey': {
            'handlers': ['applogfile',],
            'level': 'DEBUG',
        },
    }
}


EDIT

I have now discovered that my Django error logs are getting written to my Apache error logs. Is this in any way normal?

Running

sudo tail /var/log/apache2/error.log

Provides me with the expected print out that I should be getting in my above Django error log file. e.g.

[15/Dec/2014 21:36:07] DEBUG [survey:190] This is your images list in 7: ['P3D3.jpg', 'P1D1.jpg', 'P5D5.jpg']


You aren't using the correct logger in your views.py. Try this:

import logging
logger = logging.getLogger('survey')
logger.debug('This is a debug message.')

The logger you get must match the loggers defined in LOGGING.