[Django] Logging to syslog

By default, Django has following logging configuration:

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'handlers': {
        'mail_admins': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler'
        }
    },
    'loggers': {
        'django.request': {
            'handlers': ['mail_admins'],
            'level': 'ERROR',
            'propagate': True,
        },
    }
}

If I want to use custom logger like this:

import logging


my_logger = logging.getLogger('myapp.mylogger')
my_logger.info('Hi!')

I have to add the logger to LOGGING dict:

'loggers': {
    'django.request': {
        'handlers': ['mail_admins'],
        'level': 'ERROR',
        'propagate': True,
    },
    'myapp.mylogger': {
        'handlers': ['mail_admins'],
        'level': 'INFO',
        'propagate': True,
    },
}

Logging to syslog:

from logging.handlers import SysLogHandler


    'handlers': {
        ...
        'syslog': {
            'level':'INFO',
            'class':'logging.handlers.SysLogHandler',
            'formatter': 'verbose',
            'facility': SysLogHandler.LOG_LOCAL2,
            'address': '/dev/log',
        },
    },
    ...
    'loggers': {
        ...
        'myapp.mylogger': {
            'handlers': ['syslog'],
            'level': 'INFO',
            'propagate': True,
        },
    },

Logs will be available in /var/log/syslog.

Links:

Licensed under CC BY-SA 3.0