A guide to setting up Django Logging on Linux

Published by moxlotus on

Logging the events that took place in the code is important for software developers as it helps you to track down the buggy piece of code. So how can we set up the Django Logging on a linux server? Reading through the Django documentation will give you little clue on where to get started. Many who have read the documentation find it daunting, thus giving up in setting up one.

The objective of this guide is to help the fellow developers out there to quickly set-up the logging in a short time with a step by step guide.

The configuration of the logger is defined in the django settings.py file.
Copy and append the following piece of code to the settings file.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'file': {
            'level': 'INFO',
            'class': 'logging.FileHandler',
            'filename': os.path.join(BASE_DIR, 'log/info.log'),
            'formatter': 'verbose',
        },
        'rotate_file':{
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',
            'filename': os.path.join(BASE_DIR, 'log/error.log'),
            'formatter': 'verbose',
            'maxBytes': 10485760,
            'backupCount': 20,
            'encoding': 'utf8'
        },
    },
    'loggers': {
        'django': {
            'handlers': ['file'],
            'level': 'INFO',
            'propagate': True,
        },
    },
}

The above code assumes that you have the variable BASE_DIR declared at the top of the setting file which is usually a default variable found in the settings.py

So let me briefly explain what some of those terms mean there.

disable_existing_loggers : This tell the python logger whether to disable any existing logger configuration defined in other modules. It is recommended to keep it as False to avoid conflict. And it is not uncommon to have multiple logger configurations in the project.

formatters: Under this, you will define some of the output formats. Usually 2 formats are being defined. One is verbose which give you more information e.g. time of the event and module that triggered the logger. Another one which is simple that just prints the message together with the level of logging. For more information on how to customize with different formats, you may refer to theĀ Python LogRecord Attributes.

handlers: Handler is a python object that defines what is the output target of your log messages. Depending on the type of handler that you have defined, different parameters must be supplied. In this example, I have defined a FileHandler and RotatingFileHandler. In each of the handler, you may define the formatter, which was defined under formatters, to be used. Other than the 2 handlers, many other types of handlers exist. To find out more about them, refer to Logging handlers.

loggers: The logger attribute defines properties of the logger objects that are available to be used. The name of the logger will be used as the reference in the module that wants to use that particular logger object.

Now that we have our logging configuration in the django settings. Lets create the files where the log messages will go to. In our particular example, there are 2 files, info.log and error.log which resides in the folder called log

mkdir log
touch log/{info, error}.log

Execute the above commands in your django projects to create the 2 log files that will be used. After which, ensure the correct unix file permission is applied to the files and the folder to avoid any permission errors.

Now that with everything ready, we can use it in our python modules. Here is one example

import logging
logger = logging.getLogger('django')
logger.info("IT IS WORKING!!")

If you wonder how does the logger knows the configuration from the logging configuration in settings.py. Here is what actually happened(somewhere in django's source code).

import logging
import logging.config
logging.config.dictConfig(settings.LOGGING) #This is the line that loads the dictionary object from settings

I hope that everyone is able to get their logging up and running. =)

Share it with others