How to configure logging to syslog in Python?

How to configure logging to syslog in Python?

Change the line to this:

handler = SysLogHandler(address=/dev/log)

This works for me

import logging
import logging.handlers

my_logger = logging.getLogger(MyLogger)
my_logger.setLevel(logging.DEBUG)

handler = logging.handlers.SysLogHandler(address = /dev/log)

my_logger.addHandler(handler)

my_logger.debug(this is debug)
my_logger.critical(this is critical)

You should always use the local host for logging, whether to /dev/log or localhost through the TCP stack. This allows the fully RFC compliant and featureful system logging daemon to handle syslog. This eliminates the need for the remote daemon to be functional and provides the enhanced capabilities of syslog daemons such as rsyslog and syslog-ng for instance. The same philosophy goes for SMTP. Just hand it to the local SMTP software. In this case use program mode not the daemon, but its the same idea. Let the more capable software handle it. Retrying, queuing, local spooling, using TCP instead of UDP for syslog and so forth become possible. You can also [re-]configure those daemons separately from your code as it should be.

Save your coding for your application, let other software do its job in concert.

How to configure logging to syslog in Python?

I found the syslog module to make it quite easy to get the basic logging behavior you describe:

import syslog
syslog.syslog(This is a test message)
syslog.syslog(syslog.LOG_INFO, Test message at INFO priority)

There are other things you could do, too, but even just the first two lines of that will get you what youve asked for as I understand it.

Leave a Reply

Your email address will not be published.