2011-03-27 178 views
2

我發現了一個食譜做記錄一個XML-RPC服務器的活動,在此我有http://code.activestate.com/recipes/496700-logging-simplexmlrpcserver/XML-RPC服務器日誌

的問題是我想重用LoggingSimpleRPCRequestHandler(即導入),但我不不知道如何正確設置'記錄器'變量。這個想法是,

此作品(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

回答

2

好的,我沒有正確讀取你的代碼。現在我看到,在這個結構中,我無法做到我所提議的。我在這裏要做的是在模塊級準備自定義記錄器並在LoggingSimpleXMLRPCRequestHandler中按名稱檢索它。

您正在做「主」中的一部分:配置名爲「日誌」的記錄器。然後檢索記錄在LoggingSimpleXMLRPCRequestHandler

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

LoggingSimpleXMLRPCRequestHandler方法使用self.logger代替logger

+0

你能提供更多的細節嗎? 這實際上是我的問題的要點?如何將記錄器對象傳遞給我的ThreadingServer? – knguyen 2011-03-27 21:04:46

+0

對不起,以前我誤解你的代碼。請看我更新的答案。 – pajton 2011-03-27 21:42:45

+0

最後,謝謝。有用。 而不是在ctor中,我設置 logger = logging.getLogger('Log')in do_POST(self) – knguyen 2011-03-28 15:54:05