问题
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
回答1:
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
.
来源:https://stackoverflow.com/questions/5452284/xml-rpc-server-logging