19

I'm having no luck finding any information on setting up syslog logging with Django 1.3 dictionary configuration. The Django documents don't cover syslog and the python documentation is less than clear and doesn’t cover dictionary config at all. I've started with the following but I'm stuck on how to configure the SysLogHandler.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': True,
    'formatters': {
        'verbose': {
            'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s'
        },
        'simple': {
            'format': '%(levelname)s %(message)s'
        },
    },
    'handlers': {
        'syslog':{
            'level':'DEBUG',
            'class':'logging.handlers.SysLogHandler',
            'formatter': 'verbose'
        },

    },
    'loggers': {
        'django': {
            'handlers':['syslog'],
            'propagate': True,
            'level':'INFO',
        },
        'myapp': {
            'handlers': ['syslog'],
            'propagate': True,
            'level': 'DEBUG',
        },
    },
}
Rob Osborne
  • 4,897
  • 4
  • 32
  • 43

3 Answers3

22

Finally found the answer, modify the configuration in the original question to have the following for 'syslog':

from logging.handlers import SysLogHandler 
... 
        'syslog':{ 
            'level':'DEBUG', 
            'class': 'logging.handlers.SysLogHandler', 
            'formatter': 'verbose', 
            'facility': SysLogHandler.LOG_LOCAL2, 
        },
...

Warning to future generations: you pretty much have to do it exactly like the above, weird errors happen if you specify the class directly etc.

Update: I've just moved to Amazon Linux (and Django 1.5) and used the following change to the configuration for the 'syslog' section in that environment, note the 'address' argument:

    'syslog':{
        'level':'DEBUG',
        'class': 'logging.handlers.SysLogHandler',
        'formatter': 'verbose',
        'facility': 'local1',
        'address': '/dev/log',
    },
Rob Osborne
  • 4,897
  • 4
  • 32
  • 43
  • Seems to me the context here is important - 'syslog' is in handlers - right? Also the formatter won't work unless it's defined. Might be worth removing it to avoid confusion (like mine). – ostergaard Dec 27 '13 at 09:08
  • @ajostergaard Updated to make it clear these are changes to the original dictionary. Let me know if they are not more clear. – Rob Osborne Dec 31 '13 at 18:48
  • 1
    how can we give a custom tag to this ? I need to identify my log entries in rsyslog and filter them. You can do it in python logging, but how in django to do that, I dont know. Do u know ? – Luv33preet Jun 28 '17 at 14:48
10

This works for me (on default debian).

  1. I suspect using an address of /dev/log is the secret to ensure there is no attempt to use the network.
  2. I think using a logger label of '' equates to the root logger so will catch most stuff

In settings.py:

LOGGING = {
    'version': 1,
    'handlers': {
        'syslog':{
            'address': '/dev/log',
            'class': 'logging.handlers.SysLogHandler'
        }
    },
    'loggers': {
        '': {
            'handlers': ['syslog'],
            'level': 'DEBUG',
        }
    }
}

in the app:

    import logging
    logging.info("freakout info")

provides:

john:/var/log$ sudo tail -1 user.log
Dec 14 17:15:52 john freakout info
John Mee
  • 50,179
  • 34
  • 152
  • 186
0

If you're using remote syslog, be sure to include the port in the address field, otherwise you could get [Errno 9] Bad file descriptor

The logging config would look something like this

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
    "verbose": {
        "format": "%(asctime)s %(name)-12s %(levelname)-8s %(message)s",
    },
},
"handlers": {
    "console": {
        "level": "INFO",
        "class": "logging.StreamHandler",
        "stream": sys.stdout,
        "formatter": "verbose",
    },
    "syslog": {
        "level": "INFO",
        "class": "logging.handlers.SysLogHandler",
        "formatter": "verbose",
        "facility": "user",
        "address": ["192.168.1.10", 514],
}
},
"loggers": {
    "": {
        "handlers": ["console", "syslog"],
        "level": "INFO",
        "propagate": True,
    },
},

}