Search code examples
pythonloggingsyslogerror-logging

Syslog multiple facilities using python


I would like my python program to log to multiple facilities (LOCALX), where each facility entry would be for a particular condition. For example, all logging of network traffic between X and program go to LOCAL0, and between Y and the program to LOCAL1.

Anyways, I can do this quite easily with python's logging mechanism: I would import logging, and create a Sysloghandler for each facility and add it to one or more loggers.

But python also has a syslog module. I wish to use this module instead since almost all of the other python code I'm contributing to uses syslog. Is it possible and how?

The syslog documents suggest that this can't be done.

I am using python 2.7

thanks.


Solution

  • The syslog module documentation specifies that the facility can be set as an optional keyword argument to syslog.openlog([ident[, logoption[, facility]]]):

    The optional facility keyword argument (default is LOG_USER) sets the default facility for messages which do not have a facility explicitly encoded.

    You can also encode the facility in each message, as stated in the documentation of syslog.syslog(priority, message):

    Each message is tagged with a priority composed of a facility and a level. The optional priority argument, which defaults to LOG_INFO, determines the message priority. If the facility is not encoded in priority using logical-or (LOG_INFO | LOG_USER), the value given in the openlog() call is used.