Logging example for Django >1.3 on a shared hosting webserver

Every one who has already worked with Django, appreciate its useful logging information in the browser when the DEBUG modus is on while you are testing your project on a localhost developement server. But it is always helpful to have a log file stored instead of showing all of Djangos logging information in the browser, especially on a production server, where you should have usually set DEBUG = False, due to security reasons.

By default Django version >1.3 is providing logging functionality in the settings.py file, when you start a new project with python manage.py startproject <project_name>. In this short blog I will show you how I configure Django to write a readable log file on my production server, that is a shared hosting webserver without any console access:

import logging
 
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'formatter': 'verbose',
            'class': 'logging.FileHandler',
            'filename': os.path.join(CURRENT_PATH, '../django.log'),
    }
    },
    'loggers': {
        'django.request': {
            'handlers': ['file'],
            'level': 'DEBUG',
            'propagate': True,
        },
    }
}
 
# Get an instance of a logger
logger = logging.getLogger('django.request')
 
logger.debug("start logging")

This simple logging configuration is going to log in a file called django.log which stores ERROR, WARNING and INFO messages, because the lowest log level DEBUG is set. The log messages will have the following format:

ERROR 2014-08-18 14:48:29,302 base 6580 139672609781568 Internal Server Error: /de/contact/

For more information see http://docs.djangoproject.com/en/dev/topics/logging

unitec-info

You are a serious company. You deserve a serious solution.

Contact Info

unitec informatics gmbh
You want to write us?
+49 69-967-516-00
Mergenthalerallee 10-12, 65760 Eschborn
shiraznet1