Django Logging

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
  2. 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

  3. Configure rsyslog to forward Django logs to Loggly
  4. 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
    

  5. Configure Django to send logs
  6. 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',
           },
        }
    }
    

  7. Send A Test Event
  8. 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.')
    

  9. Verify Events
  10. Search Loggly for events with the syslog.appName as django over the past 20 minutes. It may take few minutes to index the event. If it doesn’t work, see the troubleshooting section below.

    syslog.appName:django
    

    django

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.
Thanks for the feedback! We'll use it to improve our support documentation.


Top