Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python SysLogHandler not working: socket.error 'Connection refused'

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:

  • I'm trying to create the Python SysLogHandler, and telling it which file to use
  • I'm telling SysLogHandler to use the DAEMON facility
  • rsyslog.conf is set up to send daemon logs to /var/log/messages (along with other logs)

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.

like image 567
sam Avatar asked Mar 22 '23 05:03

sam


1 Answers

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")
like image 163
JHill Avatar answered Apr 07 '23 04:04

JHill