Deepend
Deepend

Reputation: 4155

Django Logging not working when deployed on Ubuntu/Apache Server

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

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']

Upvotes: 2

Views: 2817

Answers (3)

sax
sax

Reputation: 3806

if you don't pass the full path of the filename, it is created in the directory the running process is started ( or make a sys/chdir), it depends on the operating system which dir is used

Upvotes: 0

slafs
slafs

Reputation: 281

My guess is that the logfile actually gets created but in a directory that you don't except it to be. Try to put a full path in your configuration. Something like 'filename': '/tmp/survey.log',, restart apache and check if it's there. Of course first you must apply a solution that Derek posted.

The reason you see the log message in your apache logs is probably because mod_wsgi configures somehow a default (root) logger and your configuration doesn't disable it (you have 'disable_existing_loggers': False,).

Upvotes: 1

Derek Kwok
Derek Kwok

Reputation: 13068

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.

Upvotes: 9

Related Questions