vendredi 8 mai 2015

How can I log from Python to syslog with either SysLogHandler or syslog on Mac OS X *and* Debian (7)

I've followed several answers here on SO to no avail.

I'm developing on a Macbook (Yosemite), but our test/production boxes are Debian 7 (using rsyslog). I'm trying to log out to syslog in a way that will work both locally and not.

I tried the option of using SysLogHandler. This works on Mac:

import logging
import logging.handlers
import syslog

h = logging.handlers.SysLogHandler(address='/var/run/syslog', facility=syslog.LOG_LOCAL1)
h.ident = 'works_on_macs'
logger = logging.getLogger('i_am_a_lumberjack')
logger.addHandler(h)

logger.debug("And I don't care")
logger.info('There is a sale on today')
logger.warn('Do not touch the hot stove!')
logger.error('Sorry, times up')
logger.critical('That sure is an ugly tie')

These messages will show up in my mac syslog. However, when I change address='/dev/log' on Debian 7... no dice.

Yet:

import syslog

syslog.openlog(ident='im_a_lumberjack', facility=syslog.LOG_LOCAL1)
syslog.syslog(syslog.WARNING, 'Watch out!')

Works on Debian 7, but not on Mac.

I would really love to be able to get one logging solution that works for both platforms. Obviously the address is going to be different, but I'm already setting that in config.

So how do I get syslog working both for Mac and Debian?

Aucun commentaire:

Enregistrer un commentaire