Skip to content

Instantly share code, notes, and snippets.

@mjnaderi
Created May 29, 2018 07:09
Show Gist options
  • Save mjnaderi/083862078422325bdc2ba809bc63eba1 to your computer and use it in GitHub Desktop.
Save mjnaderi/083862078422325bdc2ba809bc63eba1 to your computer and use it in GitHub Desktop.
Logging to Syslog in Django

source: http://www.simonkrueger.com/2015/05/27/logging-django-apps-to-syslog.html

Django conf:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s - [%(asctime)s] - %(name)s.%(funcName)s:%(lineno)s - %(message)s'
        }
    },
    'handlers': {
        'apps': {
            'level': 'DEBUG',
            'class': 'logging.handlers.SysLogHandler',
            'formatter': 'verbose',
            'facility': 'local7',
            'address': '/dev/log',
        }
    },
    'loggers': {
        'apps': {
            'handlers': ['apps'],
            'level': 'DEBUG',
            'propagate': True,
        }
    }
}

/etc/rsyslog.d/30-mydjangoproject.conf

# /etc/rsyslog.d/30-mydjangoproject.conf
local7.*                                             /var/log/mydjangoproject/apps.log
& ~  # This stops local7.* from going anywhere else.

/etc/logrotate.d/mydjangoproject

/var/log/mydjangoproject/apps.log {
    daily
    missingok
    rotate 1000
    compress
    delaycompress
    notifempty
    dateext
    dateyesterday
    dateformat .%Y-%m-%d
    extension .log
}

Create /var/log/mydjangoproject and /var/log/mydjangoproject/apps.log with appropriate permissions and restart rsyslog.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment