I'm trying to configure a Python application to log to /var/log/messages using the standard Linux syslogger. However when I try to create the syslog handler, I get the error socket.error: [Errno 111] Connection refused
.
>>> import logging
>>> import logging.handlers
>>> logger = logging.getLogger("my_logger")
>>> logger.setLevel(logging.DEBUG)
>>> handler = logging.handlers.SysLogHandler(facility=logging.handlers.SysLogHandler.LOG_DAEMON, address="/var/log/messages")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.6/logging/handlers.py", line 715, in __init__
self._connect_unixsocket(address)
File "/usr/lib64/python2.6/logging/handlers.py", line 731, in _connect_unixsocket
self.socket.connect(address)
File "<string>", line 1, in connect
socket.error: [Errno 111] Connection refused
/etc/rsyslog.conf is configured with the following line:
kern.notice;*.err;local0.info;mail.none;authpriv.none;cron.none;local1.none;daemon.notice /var/log/messages
I'm guessing the problem is somewhere in the configuration of syslog, but as far as I can see I've done the right things:
Is there something else I should be doing to make this work?
Previously I just used a logging.FileHandler
but this doesn't work properly as when the messages file wraps, Python continues logging to the old file.
You need to point to the actual unix domain socket, not the log file. Python docs
Try this:
import logging
import logging.handlers
logger = logging.getLogger("my_logger")
logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(
facility=logging.handlers.SysLogHandler.LOG_DAEMON, address="/dev/log")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With