Catching errors when logging with SocketHandler in Python

折月煮酒 提交于 2019-12-07 14:31:53

问题


My web application runs on multpile apache instances and I am having multiprocess logging issues because of this. I am currently using a SocketHandler for logging to a daemon using SocketServer that then writes logs to a single log file (similar to this example).

Now that I am using a SocketHandler for logging I am having trouble discovering if/when the socket server crashes. For example, if I try creating a SocketHandler for a port that has no listening socket server, no exception arises. I would like to catch this type of error and log it to a file.

My question is, when logging using SocketHandler how can I discover when the socket being used is not currently being listened to?


回答1:


When a socket creation operation fails (e.g. because there is no server listening), the default behaviour is to retry the next time an event is logged, with an exponential back-off algorithm. Here are some approaches you could try:

  1. Subclass SocketHandler, override the createSocket method and handle exceptions how you will in your implementation.
  2. Note that the sock attribute of the SocketHandler instance will be None if no socket has been created yet. If the value is None after you've logged an event, most likely the SocketHandler wouldn't have sent it.
  3. Note that the makeSocket method of the handler is used to actually create and connect the socket. So, you could call makeSocket yourself before logging anything, and if it throws an exception, you know that your server is probably not listening. If it returns success, then you can just close the returned value.
  4. Subclass SocketHandler, override the emit method, and in your subclass, have a reference to an alternate handler instance (e.g. FileHandler or SMTPHandler) which you want to use. In your emit code, try to create the socket using

    if self.sock is None:
        self.createSocket()
    if self.sock is None:
        # creation failed: do self.alternate_handler.handle(record)
    else:
        # creation succeeded: defer to superclass implementation
    

Of course, this may not catch any errors that occur if the server goes down in the middle of sending a message, but it should at least alert you to some problems with your servers.




回答2:


There is no way to do that with the current resilient implementation. "Resilient" means in this case that the SocketHandler will handle problems with the socket already by trying to reopen it as soon as there is a problem.

Just restart the server and the handlers will reconnect eventually.



来源:https://stackoverflow.com/questions/2220159/catching-errors-when-logging-with-sockethandler-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!