Documentation forLoggly

Django Logging

Loggly provides the infrastructure to aggregate and normalize log events so they are available to explore interactively, build visualizations, or create threshold-based alerting. In general, any method to send logs from a system or application to an external source can be adapted to send logs to Loggly. The following instructions provide one scenario for sending logs to Loggly.

You can send Django logs to Loggly over syslog. This will send them from Django to a local rsyslog daemon which will forward them to Loggly. It assumes you use rsyslog 1.19 or higher in the standard directory, TCP over port 514, and you have sudo permissions. For alternatives, please see the Advanced Options section below.

Django Logging Setup

1. Configure syslog daemon

Run our automatic configure-linux script below to setup logging and send the Django logs to Loggly through your syslog daemon.

curl -O https://www.loggly.com/install/configure-linux.sh
sudo bash configure-linux.sh -a SUBDOMAIN -u USERNAME

Replace:

  • SUBDOMAIN: your account subdomain that you created when you signed up for Loggly
  • USERNAME: your Loggly username

2. Configure rsyslog to forward Django logs to Loggly

Open your Django configuration file for rsyslog or create a new one:

sudo vim /etc/rsyslog.d/21-django.conf

Paste this configuration into the file then save it.

#provides UDP syslog reception
$ModLoad imudp
$UDPServerRun 514
#Add a tag for Django events
$template LogglyFormatDjango,"<%pri%>%protocol-version% %timestamp:::date-rfc3339% %HOSTNAME% %app-name% %procid% %msgid% [TOKEN@41058 tag=\"Django\"] %msg%\n"
local7.* @@logs-01.loggly.com:514;LogglyFormatDjango
local7.* ~ #discards the messages so they don't end up in /var/log/syslog

Replace:

Then restart rsyslog so the changes take effect

sudo service rsyslog restart

3. Configure Django to send logs

Open settings.py file of your application.

vim settings.py 

Copy the following code in the file to configure your Django application to send logs to Loggly

LOGGING = {
  'version': 1,
  'disable_existing_loggers': False,
  'formatters': {
    'django': { 
      'format':'django: %(message)s',
    },
  },
  'handlers': {
    'logging.handlers.SysLogHandler': {
      'level': 'DEBUG',
      'class': 'logging.handlers.SysLogHandler',
      'facility': 'local7',
      'formatter': 'django',
      'address' : '/dev/log',
    },
  },
  'loggers': {
    'loggly_logs':{
      'handlers': ['logging.handlers.SysLogHandler'],
      'propagate': True,
      'format':'django: %(message)s',
      'level': 'DEBUG',
    },
  }
}

4. Send A Test Event

Now call the defined logger in the Django files. In our case it is loggly_logs. Run your application with this code to send a test event to Loggly.

import logging
logger = logging.getLogger('loggly_logs')
logger.info('Hi, Welcome to Loggly.')

5. Verify Events

Search Loggly for events with the logtype as django over the past 20 minutes. It may take few minutes to index the event. If it doesn’t work, see the Django Logging Troubleshooting section below.

logtype:django

Django Example

Advanced Django Logs Options

Django Logging Troubleshooting

If you don’t see any data show up in the verification step, then check for these common problems.

  • Wait a few minutes in case indexing needs to catch up
  • Check if you are using the same logger name as mentioned in the settings.py file.
  • Troubleshooting Rsyslog
  • Search or post your own questions around Django logging, Python logging, Django error logs, plus logging messages and default and custom logging configuration in the community forum.

The scripts are not supported under any SolarWinds support program or service. The scripts are provided AS IS without warranty of any kind. SolarWinds further disclaims all warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The risk arising out of the use or performance of the scripts and documentation stays with you. In no event shall SolarWinds or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the scripts or documentation.