XML-RPC 服务器日志记录
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我还没有正确阅读你的代码。现在我发现在这种结构中不可能按照我的建议去做。我在这里要做的是在模块级别准备自定义记录器,并在 LoggingSimpleXMLRPCRequestHandler 中按名称检索它。
您正在“main”中完成其中的一部分:使用名称“Log”配置记录器。然后在
LoggingSimpleXMLRPCRequestHandler
中检索该记录器:并在
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
:and in
LoggingSimpleXMLRPCRequestHandler
methods useself.logger
instead oflogger
.