Django Broken pipe in Debug mode

前端 未结 8 1106
灰色年华
灰色年华 2020-11-28 23:45

I have django 1.3 on the remote server behind Nginx.

If I run django with apache + mod_wsgi, I can watch errors in apache log files. It\'s ok but I\'d like to have

8条回答
  •  死守一世寂寞
    2020-11-29 00:11

    Here is a way to prevent the to print the message to stderr. Just monkey patch the BaseServer.handle_error function. This is how I do it:

    def patch_broken_pipe_error():
        """Monkey Patch BaseServer.handle_error to not write
        a stacktrace to stderr on broken pipe.
        https://stackoverflow.com/a/7913160"""
        import sys
        from SocketServer import BaseServer
    
        handle_error = BaseServer.handle_error
    
        def my_handle_error(self, request, client_address):
            type, err, tb = sys.exc_info()
            # there might be better ways to detect the specific erro
            if repr(err) == "error(32, 'Broken pipe')":
                # you may ignore it...
                logging.getLogger('mylog').warn(err)
            else:
                handle_error(self, request, client_address)
    
        BaseServer.handle_error = my_handle_error
    
    
    patch_broken_pipe_error()
    

提交回复
热议问题