4

我正在尝试将 Python 应用程序配置为使用标准 Linux syslogger 登录到 /var/log/messages。但是,当我尝试创建 syslog 处理程序时,出现错误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 配置有以下行:

kern.notice;*.err;local0.info;mail.none;authpriv.none;cron.none;local1.none;daemon.notice /var/log/messages

我猜问题出在系统日志的配置中,但据我所知,我已经做了正确的事情:

  • 我正在尝试创建 Python SysLogHandler,并告诉它使用哪个文件
  • 我告诉 SysLogHandler 使用 DAEMON 工具
  • rsyslog.conf 设置为将守护程序日志发送到 /var/log/messages(以及其他日志)

我还应该做些什么来完成这项工作吗?

以前我只使用了 a logging.FileHandler,但这不能正常工作,因为当消息文件换行时,Python 会继续记录到旧文件。

4

1 回答 1

7

您需要指向实际的 unix 域套接字,而不是日志文件。Python 文档

尝试这个:

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")
于 2013-10-02T14:22:28.270 回答