基于 TCP 的 Python SysLogHandler:处理连接丢失

Posted

技术标签:

【中文标题】基于 TCP 的 Python SysLogHandler:处理连接丢失【英文标题】:Python SysLogHandler over TCP: handling connection loss 【发布时间】:2016-10-17 16:34:59 【问题描述】:

我有一个进程使用 logging.SyslogHandler 通过 TCP 将日志发送到 syslog 服务器。 不幸的是,如果系统日志服务器由于某种原因重新启动,该进程将停止发送日志并且无法重新建立连接。

我想知道是否有人知道克服这种行为并强制 logging.SyslogHandler 重新建立连接的方法。

使用处理程序的代码类似于

import logging
import logging.handlers
import logging.config

logging.config.fileConfig('logging.cfg')
logging.debug("debug log message")

logging.cfg

[loggers]
keys=root,remote

[handlers]
keys=local,remote

[logger_remote]
qualname=remote
level=INFO
handlers=remote

[logger_root]
qualname=root
level=DEBUG
handlers=local

[handler_local]
class=handlers.StreamHandler
level=DEBUG
formatter=local
args=(sys.stdout,)

[handler_remote]
class=handlers.SysLogHandler
level=DEBUG
formatter=remote
args=(('localhost', 514), handlers.SysLogHandler.LOG_USER, 1)

[formatters]
keys=local,remote

[formatter_local]
format=%(module)s %(levelname)s %(message)s

[formatter_remote]
format=%(asctime)s %(message)s

系统日志服务器重新启动后我不断收到的错误是:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/logging/handlers.py", line 866, in emit
    self.socket.sendall(msg)
  File "/usr/local/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
error: [Errno 32] Broken pipe

如果有任何见解,我将不胜感激。 谢谢!

【问题讨论】:

('localhost', 514) 意思是“你好我的root朋友你好吗?谁告诉你可以使用514端口?(只能用作目标,不能用作源)” 【参考方案1】:

我遇到了同样的问题。我必须编写一个自定义处理程序来处理损坏的管道异常并重新创建套接字。

class ReconnectingSysLogHandler(logging.handlers.SysLogHandler):                
    """Syslog handler that reconnects if the socket closes                      

    If we're writing to syslog with TCP and syslog restarts, the old TCP socket 
    will no longer be writeable and we'll get a socket.error of type 32.  When  
    than happens, use the default error handling, but also try to reconnect to  
    the same host/port used before.  Also make 1 attempt to re-send the         
    message.                                                                    
    """                                                                         
    def __init__(self, *args, **kwargs):                                        
        super(ReconnectingSysLogHandler, self).__init__(*args, **kwargs)        
        self._is_retry = False                                                  

    def _reconnect(self):                                                       
        """Make a new socket that is the same as the old one"""                 
        # close the existing socket before getting a new one to the same host/port
        if self.socket:                                                         
            self.socket.close()                                                 

        # cut/pasted from logging.handlers.SysLogHandler                        
        if self.unixsocket:                                                     
            self._connect_unixsocket(self.address)                              
        else:                                                                   
            self.socket = socket.socket(socket.AF_INET, self.socktype)          
            if self.socktype == socket.SOCK_STREAM:                             
                self.socket.connect(self.address)                               

    def handleError(self, record):                                              
        # use the default error handling (writes an error message to stderr)    
        super(ReconnectingSysLogHandler, self).handleError(record)              

        # If we get an error within a retry, just return.  We don't want an     
        # infinite, recursive loop telling us something is broken.              
        # This leaves the socket broken.                                        
        if self._is_retry:                                                      
            return                                                              

        # Set the retry flag and begin deciding if this is a closed socket, and  
        # trying to reconnect.                                                  
        self._is_retry = True                                                   
        try:                                                                    
            __, exception, __ = sys.exc_info()                                  
            # If the error is a broken pipe exception (32), get a new socket.   
            if isinstance(exception, socket.error) and exception.errno == 32:   
                try:                                                            
                    self._reconnect()                                           
                except:                                                         
                    # If reconnecting fails, give up.                           
                    pass                                                        
                else:                                                           
                    # Make an effort to rescue the recod.                       
                    self.emit(record)                                           
        finally:                                                                
            self._is_retry = False

【讨论】:

老兄,你完全拯救了这一天。【参考方案2】:

您可能只需要检查该错误并在出现时重新建立连接。请尝试以下操作:

try:
    logging.debug("debug log message")
except IOError, e:
    if e.errno == 32:
       logging.config.fileConfig('logging.cfg')
       logging.debug("debug log message")

【讨论】:

把它变成一个函数 debug(message) 可能是有意义的。 在你所有的日志调用中使用 try/catch 听起来真的很讨厌。就像@MKesper 说的那样,您可以将所有这些都烘焙到一个函数中,但是您使用的任何记录日志的库都不会从这个 try/catch 中受益。

以上是关于基于 TCP 的 Python SysLogHandler:处理连接丢失的主要内容,如果未能解决你的问题,请参考以下文章

python基础22------python基础之基于tcp和udp的套接字

Python网络编程02/基于TCP协议的socket简单的通信

Python 基于TCP协议通信的简单套接字编程

Python 基于TCP协议通信的简单套接字编程

python中基于tcp协议的通信(数据传输)

基于 TCP 的 Python SysLogHandler:处理连接丢失