Django Logging Configuration Example

Ship logs from Django applications to Logit.io

Follow the steps below to send your observability data to Logit.io

Logs

To send logs to Logit.io via Django you will need the python-logstash-async (version 2.5.0) Python plugin.

Install Integration

Please click on the Install Integration button to configure your stack for this source.

Installing the Python logging handler

Execute the following command using pip (the official Python package installer) to install the logging handler. This sends Python logging events to Logstash, which is part of your Logit.io log management stack:

pip install python-logstash-async

If you receive errors suggesting pip is an unknown command, navigate to its installed location with your chosen terminal program and try again (or add pip to your environmental variables if using Windows).

Sending Logs

Add your imports to the top of your file (the file you wish to include logging on):

import logging
from logstash_async.handler import AsynchronousLogstashHandler
from logstash_async.handler import LogstashFormatter

Add this code somewhere inside the same file, for example:

def main():
 
    # Create the logger and set it's logging level
    logger = logging.getLogger("logstash")
    logger.setLevel(logging.DEBUG)        
    
    # Create the handler
    handler = AsynchronousLogstashHandler(
        host='@logstash.host', 
        port='@logstash.sslPort:strip_quotes',
        ssl_enable=True, 
        ssl_verify=False,
        transport='logstash_async.transport.BeatsTransport',
        database_path='')
 
    # Here you can specify additional formatting on your log record/message
    formatter = LogstashFormatter()
    handler.setFormatter(formatter)
    
    # Assign handler to the logger
    logger.addHandler(handler)
    
    # Send log records to Logstash 
    logger.error('django-logstash-async: django error message.')
    logger.info('django-logstash-async: django info message.')
    logger.warning('django-logstash-async: django warning message.')
    logger.debug('django-logstash-async: django debug message.')
 
if __name__ == '__main__':
    main()

Launch Logs to View Data

Launch Logs

More Help

If you need any further assistance with migrating your Django application logs (opens in a new tab) to Logit.io we're here to help you get started. Just get in contact with our support team via live chat & we'll be happy to assist.