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

1

13

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?

Edit:

As further information - I've discovered that my SysLogHandler seems to maybe not be using the facility(?) right. Messages are being picked up by syslog, but they're going to a catch-all, which makes me believe they're not getting tagged with LOG_LOCAL1

Sheree answered 8/5, 2015 at 13:44 Comment(0)
S
14

Turns out the facility that SysLogHandler expects is not syslog.LOG_LOCAL1 or any of the values in that namespace.

It expects 'local1' or other string as explained in the documentation.

Simply changing

h = logging.handlers.SysLogHandler(address='/var/run/syslog', facility=syslog.LOG_LOCAL1)

to

h = logging.handlers.SysLogHandler(address='/var/run/syslog', facility='local1')

Made everything right on both systems.

Sheree answered 8/5, 2015 at 15:49 Comment(2)
If this was the case, it seems to have been fixed in python's code: def encodePriority(self, facility, priority): if isinstance(facility, basestring): facility = self.facility_names[facility] Roomer
That seems accurate. I'm sure it's been a while since that's actually been a problem... at least I hope :DSheree

© 2022 - 2024 — McMap. All rights reserved.