How to configure syslog for logging in Python?

How to configure logging to syslog in python?

  • I can't get my head around Python's logging module. My needs are very simple: I just want to log everything to syslog. After reading documentation I came up with this simple test script: import logging import logging.handlers my_logger = logging.getLogger('MyLogger') my_logger.setLevel(logging.DEBUG) handler = logging.handlers.SysLogHandler() my_logger.addHandler(handler) my_logger.debug('this is debug') my_logger.critical('this is critical') But this script does not produce any log records in syslog. What's wrong?

  • Answer:

    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')

thor at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

Is your syslog.conf set up to handle facility=user? You can set the facility used by the python logger with the facility argument, something like this: handler = logging.handlers.SysLogHandler(facility=SysLogHandler.LOG_DAEMON)

bstpierre

I add a little extra comment just in case it helps anyone because I found this exchange useful but needed this little extra bit of info to get it all working. To log to a specific facility using SysLogHandler you need to specify the facility value. Say for example that you have defined: local3.* /var/log/mylog in syslog, then you'll want to use: handler = logging.handlers.SysLogHandler(address = ('localhost',514), facility=19) and you also need to have syslog listening on UDP to use localhost instead of /dev/log.

Oliver Henriot

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.