2

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

3 Answers 3

9
+50

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That makes perfect sense now. Unfortunately it must not be the only problem as it is still not working. Any other suggestions? Thanks
As slafs said, try using an absolute path for your logging file. The line of code you've commented out should work os.path.join(DJANGO_ROOT, 'survey.log')
Thanks, I also had to set write permissions on my survey.log file to get it working in the end
1

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,).

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.