XML-RPC 服务器日志记录

发布于 2024-10-26 12:15:42 字数 3497 浏览 3 评论 0原文

我在 http://code 找到了一个记录 XML-RPC 服务器活动的配方。 activestate.com/recipes/496700-logging-simplexmlrpcserver/

我遇到的问题是我想重用 LoggingSimpleRPCRequestHandler (即导入它),但我不知道如何正确设置“logger”变量。这个想法是,

这可行(LoggedWork.py):

from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from SocketServer import ThreadingMixIn

import os, sys
import logging

class RemoteObject:    
    def return10(self):
        return 10

class LoggingSimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): 
    """Overides the default SimpleXMLRPCRequestHander to support logging.  Logs
    client IP and the XML request and response.
    """    

    def do_POST(self):
        clientIP, port = self.client_address
    # Log client IP and Port

        logger.info('Client IP: %s - Port: %s' % (clientIP, port))
        try:
            # get arguments
            data = self.rfile.read(int(self.headers["content-length"]))
            # Log client request
        logger.info('Client request: \n%s\n' % data)

            response = self.server._marshaled_dispatch(
                    data, getattr(self, '_dispatch', None)
                )
        # Log server response
            logger.info('Server response: \n%s\n' % response)

    except: # This should only happen if the module is buggy
            # internal error, report as HTTP server error
            self.send_response(500)
            self.end_headers()
        else:
            # got a valid XML RPC response
            self.send_response(200)
            self.send_header("Content-type", "text/xml")
            self.send_header("Content-length", str(len(response)))
            self.end_headers()
            self.wfile.write(response)

            # shut down the connection
            self.wfile.flush()
            self.connection.shutdown(1)

class ThreadingServer(ThreadingMixIn, SimpleXMLRPCServer):
    pass

if __name__ == "__main__":

    logger = logging.getLogger('Log')
    hdlr = logging.FileHandler('Log.log')
    formatter = logging.Formatter("%(asctime)s  %(levelname)s  %(message)s")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)


    serveraddr = ('', 10001)
    srvr       = ThreadingServer(serveraddr, LoggingSimpleXMLRPCRequestHandler)    
    srvr.register_instance(RemoteObject())
    srvr.register_introspection_functions()

    srvr.serve_forever()

而这不行(LoggedBroken.py):

from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from SocketServer import ThreadingMixIn
from  LoggingSimpleXMLRPCRequestHandler import  LoggingSimpleXMLRPCRequestHandler
import os, sys
import logging

class RemoteObject:    
    def return10(self):
        return 10

class ThreadingServer(ThreadingMixIn, SimpleXMLRPCServer):
    pass

if __name__ == "__main__":

    logger = logging.getLogger('Log')
    hdlr = logging.FileHandler('Log.log')
    formatter = logging.Formatter("%(asctime)s  %(levelname)s  %(message)s")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)


    serveraddr = ('', 10001)
    srvr       = ThreadingServer(serveraddr, LoggingSimpleXMLRPCRequestHandler)    
    srvr.register_instance(RemoteObject())
    srvr.register_introspection_functions()

    srvr.serve_forever()

如果有更好的方法,请告知。谢谢。

-k

I found a recipe do log activities of an XML-RPC server at http://code.activestate.com/recipes/496700-logging-simplexmlrpcserver/

The problem that I'm having is I want to reuse the LoggingSimpleRPCRequestHandler (i.e import it) but I don't know how to correctly set the 'logger' variable. The idea is that

This works (LoggedWork.py):

from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from SocketServer import ThreadingMixIn

import os, sys
import logging

class RemoteObject:    
    def return10(self):
        return 10

class LoggingSimpleXMLRPCRequestHandler(SimpleXMLRPCRequestHandler): 
    """Overides the default SimpleXMLRPCRequestHander to support logging.  Logs
    client IP and the XML request and response.
    """    

    def do_POST(self):
        clientIP, port = self.client_address
    # Log client IP and Port

        logger.info('Client IP: %s - Port: %s' % (clientIP, port))
        try:
            # get arguments
            data = self.rfile.read(int(self.headers["content-length"]))
            # Log client request
        logger.info('Client request: \n%s\n' % data)

            response = self.server._marshaled_dispatch(
                    data, getattr(self, '_dispatch', None)
                )
        # Log server response
            logger.info('Server response: \n%s\n' % response)

    except: # This should only happen if the module is buggy
            # internal error, report as HTTP server error
            self.send_response(500)
            self.end_headers()
        else:
            # got a valid XML RPC response
            self.send_response(200)
            self.send_header("Content-type", "text/xml")
            self.send_header("Content-length", str(len(response)))
            self.end_headers()
            self.wfile.write(response)

            # shut down the connection
            self.wfile.flush()
            self.connection.shutdown(1)

class ThreadingServer(ThreadingMixIn, SimpleXMLRPCServer):
    pass

if __name__ == "__main__":

    logger = logging.getLogger('Log')
    hdlr = logging.FileHandler('Log.log')
    formatter = logging.Formatter("%(asctime)s  %(levelname)s  %(message)s")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)


    serveraddr = ('', 10001)
    srvr       = ThreadingServer(serveraddr, LoggingSimpleXMLRPCRequestHandler)    
    srvr.register_instance(RemoteObject())
    srvr.register_introspection_functions()

    srvr.serve_forever()

and this doesn't (LoggedBroken.py):

from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler
from SocketServer import ThreadingMixIn
from  LoggingSimpleXMLRPCRequestHandler import  LoggingSimpleXMLRPCRequestHandler
import os, sys
import logging

class RemoteObject:    
    def return10(self):
        return 10

class ThreadingServer(ThreadingMixIn, SimpleXMLRPCServer):
    pass

if __name__ == "__main__":

    logger = logging.getLogger('Log')
    hdlr = logging.FileHandler('Log.log')
    formatter = logging.Formatter("%(asctime)s  %(levelname)s  %(message)s")
    hdlr.setFormatter(formatter)
    logger.addHandler(hdlr)
    logger.setLevel(logging.INFO)


    serveraddr = ('', 10001)
    srvr       = ThreadingServer(serveraddr, LoggingSimpleXMLRPCRequestHandler)    
    srvr.register_instance(RemoteObject())
    srvr.register_introspection_functions()

    srvr.serve_forever()

If there is a better way to do, please advise. Thank you.

-k

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木格 2024-11-02 12:15:42

好吧,我还没有正确阅读你的代码。现在我发现在这种结构中不可能按照我的建议去做。我在这里要做的是在模块级别准备自定义记录器,并在 LoggingSimpleXMLRPCRequestHandler 中按名称检索它。

您正在“main”中完成其中的一部分:使用名称“Log”配置记录器。然后在 LoggingSimpleXMLRPCRequestHandler 中检索该记录器:

class LoggingSimpleXMLRPCRequestHandler(SimpleXMLRequestHandler):
    def __init__(self):
        self.logger = logging.getLogger('Log')

并在 LoggingSimpleXMLRPCRequestHandler 方法中使用 self.logger 而不是 logger

Ok, I haven't properly read you code. Now I see that in this structure it is impossible to do what I proposed. What I would do here is to prepare custom logger at the module level and retrieve it by name in LoggingSimpleXMLRPCRequestHandler.

Your are doing part of it in "main": configuring the logger with name 'Log'. Then retrieve that logger in LoggingSimpleXMLRPCRequestHandler:

class LoggingSimpleXMLRPCRequestHandler(SimpleXMLRequestHandler):
    def __init__(self):
        self.logger = logging.getLogger('Log')

and in LoggingSimpleXMLRPCRequestHandler methods use self.logger instead of logger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文