Seeking a Django 1.3 and syslog configuration example

1,121 views
Skip to first unread message

Rob

unread,
Jun 1, 2011, 11:58:43 AM6/1/11
to Django users
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',
},
},
}

Shawn Milochik

unread,
Jun 1, 2011, 12:41:52 PM6/1/11
to django...@googlegroups.com
This should help out. It's for a file, not SysLogHandler, but the idea
is the same.

Just take any arguments you would normally pass to the handler if you
were creating it programmatically and add them as keys to the dictionary:

'log_file': {
'level': 'DEBUG',
'class': 'logging.handlers.RotatingFileHandler',
'filename': '/tmp/myfilename.log',
'formatter': 'verbose',
'backupCount': 50,
'maxBytes': 2 ** 20,
},

As demonstrated here, filename, backupCount, and maxBytes (used by the
RotatingFileHandler but not in Django's default logging dict) were just
added and given values. Do the same for SysLogHandler parameters and it
should work.

I had the same difficulty you were having in figuring this out; we
should update the documentation to mention this. I'll open a ticket.

Shawn


Matteius

unread,
Jun 2, 2011, 1:48:20 PM6/2/11
to Django users
# Configure Project Logging using Django Logging setting and
specifying
# Dict-Config to Python 1.6
LOGGING = {
'version': 1,
'formatters': {
'simple': {
'format': '%(asctime)s %(levelname)s %(module)s [%(name)s]
- %(message)s \n',
},
'verbose': {
'format': '%(asctime)s %(levelname)s %(module)s %
(process)d %(thread)d %(message)s'
},
},
'handlers': {
'log_test': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/log/classcomm/log-test.log',
'when': 'H',
'interval': 1,
'backupCount': 5,
'formatter': 'simple',
},
'classcomm': {
'class': 'logging.handlers.TimedRotatingFileHandler',
'filename': '/var/log/classcomm/classcomm.log',
'when': 'H',
'interval': 1,
'backupCount': 5,
'formatter': 'verbose',
},
'student_portal': {
'class': 'logging.FileHandler',
'filename': '/var/log/classcomm/student_portal.log',
'mode': 'a',
'formatter': 'verbose',
},
'instructor_portal': {
'class': 'logging.FileHandler',
'filename': '/var/log/classcomm/instructor_portal.log',
'mode': 'a',
'formatter': 'verbose',
},
'django': {
'class': 'logging.FileHandler',
'filename': '/var/log/classcomm/django.log',
'mode': 'a',
'formatter': 'verbose',
},
'django_sql': {
'class': 'logging.FileHandler',
'filename': '/var/log/classcomm/django-sql.log',
'mode': 'a',
'formatter': 'verbose',
},
# 'sentry_handler': {
# 'class': 'sentry.client.handlers.SentryHandler',
# 'formatter': 'verbose',
# },
# 'stream_handler': {
# 'class': 'logging.StreamHandler',
# 'formatter': 'verbose',
# }
},
# Root logger (complete logging)
'root' : { 'level' : 'WARNING', 'handlers' : ['classcomm'], #,
'sentry_handler'],
},
'loggers': {
'log_test': { 'level': 'INFO', 'handlers': ['log_test'] },
'student_portal': { 'level': 'INFO', 'handlers':
['student_portal'] },
'instructor_portal': { 'level': 'INFO', 'handlers':
['instructor_portal'] },
'django': { 'handlers': ['django'] },
'django.core.urlresolvers': { 'level': 'DEBUG' },
'django.core.handlers.base': { 'level': 'DEBUG' },
'django.db.models.loading': { 'level': 'DEBUG' },
'django.db.backends.util': { 'level': 'DEBUG',
'propagate': False,
'handlers': ['django_sql'] },
# 'sentry.errors': { 'level': 'INFO', 'handlers':
['stream_handler'] },
},
}

Rob

unread,
Jun 3, 2011, 11:02:45 AM6/3/11
to Django users
Thanks, but this doesn't help at all. I have no problem getting the
RotatingFileHandler working, it is documented albeit poorly, but the
SysLogHandler isn't documented at all and doesn't work the same way.

The SysLogHandler actually has to connect to syslogd and log to a
syslog facility (eg. LOG_USER or LOG_LOCAL0). How do I configure
those
options?

Rob.

Piotr Zalewa

unread,
Jun 3, 2011, 11:14:39 AM6/3/11
to django...@googlegroups.com
In Add-ons Builder I've got something like this in log_settings.py

https://github.com/mozilla/FlightDeck/blob/master/log_settings.py

'handlers': {
'syslog': {
'()': logging.handlers.SysLogHandler,
'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
'formatter': 'prod',
},
}

It depends on the commonware, but I guess it's nothing there regarding
logging to the SysLog
https://github.com/jsocol/commonware/

Good luck
zalun

Rob

unread,
Jun 3, 2011, 12:06:02 PM6/3/11
to Django users
Well that worked but I'm not happy about it. I have the following:

from logging.handlers import SysLogHandler

...

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

If I specify 'class' as SysLogHandler instead of
'logging.handlers.SysLogHandler' I get an error:

ValueError: Unable to configure handler 'syslog': class SysLogHandler
has no attribute 'split'

If I just import logging and use
logging.handlers.SysLogHandler.LOG_LOCAL2 I get another error:

AttributeError: 'module' object has no attribute 'handlers'

Very bizarre behaviour all around but I'm up and running. Thanks for
everybody's help!

On Jun 3, 11:14 am, Piotr Zalewa <zal...@gmail.com> wrote:
> In Add-ons Builder I've got something like this in log_settings.py
>
> https://github.com/mozilla/FlightDeck/blob/master/log_settings.py
>
>      'handlers': {
>          'syslog': {
>              '()': logging.handlers.SysLogHandler,
>              'facility': logging.handlers.SysLogHandler.LOG_LOCAL7,
>              'formatter': 'prod',
>          },
>      }
>
> It depends on the commonware, but I guess it's nothing there regarding
> logging to the SysLoghttps://github.com/jsocol/commonware/
Reply all
Reply to author
Forward
0 new messages