2013-10-02 43 views
4

我想配置一個Python應用程序使用標準的Linux系統日誌記錄到/ var/log/messages。但是,當我嘗試創建系統日誌處理程序時,出現錯誤socket.error: [Errno 111] Connection refusedPython的SysLogHandler不工作:socket.error'連接被拒絕'

>>> 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 FACI lity
  • rsyslog.conf設置爲後臺日誌發送到/ var/log/messages中(連同其它日誌)

有別的我應該做的,使這項工作?

以前我只使用了logging.FileHandler,但是當消息文件環繞時,Python不能正常工作,Python會繼續記錄到舊文件。

回答

6

您需要指向實際的unix域套接字,而不是日誌文件。 Python docs

試試這個:

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") 
+0

真棒,謝謝。當他們說「解決你應該使用的域套接字」時,我沒有意識到這些文檔意味着什麼 - 現在有道理。謝謝! – sam