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
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:
- How To Configure Your Freedom?Best solution by Yahoo! Answers
- How to configure android kernel?Best solution by Stack Overflow
- How to configure nginx for JBoss?Best solution by Server Fault
- How to configure dedicated server?Best solution by Server Fault
- How to configure outlook 2007 for Yahoo mail?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.