"Protocol wrong type for socket" 在日志记录配置中使用 socket.SOCK_STREAM 时

"Protocol wrong type for socket" when using socket.SOCK_STREAM in logging configuration

我在我的日志配置文件中使用 SysLogHandler

因为 rsyslog 在我的 linux 框中是 运行,我想使用 socket.SOCK_STREAM 作为 SysLogHandlersocktype 关键字参数实例。 documentation 确实说:

To open a TCP socket (for use with the newer syslog daemons such as rsyslog), specify a value of socket.SOCK_STREAM.

但是当我添加处理程序时,像这样:

log.addHandler(logging.handlers.SysLogHandler(address='/dev/log',
                                          facility='mathmaker',
                                          socktype=socket.SOCK_STREAM))

我收到这个错误:

  File "/home/zezo/dev/myapp/myapp", line 52, in <module>
    socktype=socket.SOCK_STREAM))
  File "/usr/lib/python3.4/logging/handlers.py", line 803, in __init__
    self._connect_unixsocket(address)
  File "/usr/lib/python3.4/logging/handlers.py", line 820, in _connect_unixsocket
    self.socket.connect(address)
OSError: [Errno 91] Protocol wrong type for socket

虽然 rsyslog 似乎正在监听 /dev/log:lsof | grep rsyslog 显示:

rsyslogd   8090           syslog    0u     unix 0xffff8800b413ce00      0t0    2739240 /dev/log

看起来无法将 socket.SOCK_STREAM 用于 unix 套接字。

我觉得我读文档的速度太快了,如:

for use with the newer syslog daemons such as rsyslog

并不意味着 rsyslog 被配置为使用 TCP 套接字(只是它可能是)。