[英]Weird Characters Received Using Python Logging Socket Handler
我正在使用python(2.7),需要将消息记录到远程服务器。 我正在使用logging.handlers库,但是消息变得奇怪了。 现在,我正在使用python文档网站上提供的SocketServer代码。
import logging
import logging.handlers
class TestMan():
def __init__(self,xmlString):
formatter = logging.Formatter('%(levelname)s : %(name)s - %(asctime)s - %(message)s')
logging.basicConfig(filename='test.log',level=logging.DEBUG,format='%(levelname)s : %(name)s - %(asctime)s - %(message)s')
log = logging.getLogger(__name__)
socket = logging.handlers.SocketHandler('localhost',1030)
socket.setLevel(logging.INFO)
socket.setFormatter(formatter)
log.addHandler(socket)
log.info("Starting Test")
收到以下消息(无法粘贴文本,因此进行了屏幕截图)
http://i.imgur.com/W65K6iC.png
服务器代码:
import SocketServer
class MyTCPHandler(SocketServer.BaseRequestHandler):
"""
The RequestHandler class for our server.
It is instantiated once per connection to the server, and must
override the handle() method to implement communication to the
client.
"""
def handle(self):
# self.request is the TCP socket connected to the client
self.data = self.request.recv(1024).strip()
print "{} wrote:".format(self.client_address[0])
print self.data
# just send back the same data, but upper-cased
self.request.sendall(self.data.upper())
if __name__ == "__main__":
HOST, PORT = "localhost", 1030
# Create the server, binding to localhost on port 9999
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
# Activate the server; this will keep running until you
# interrupt the program with Ctrl-C
server.serve_forever()
由SocketHandlers发送的数据在发送之前先经过腌制。
https://docs.python.org/2/library/logging.handlers.html#logging.handlers.SocketHandler
generate()腌制记录的属性字典,并将其以二进制格式写入套接字。 如果套接字有错误,请静默丢弃数据包。 如果以前丢失了连接,请重新建立连接。 要将接收端的记录释放到LogRecord中,请使用makeLogRecord()函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.