如何通过套接字检索 python.logging 日志记录



作为我测试的一部分,尝试检查套接字是否收到日志。
因此,线程发送日志,主线程尝试在套接字处检索。

无法将接收到的数据转换为日志记录。
按照 Python 2.7 文档尝试logging.makeLogRecord。也尝试了pickle/cPickle

import unittest
import socketLogger
import logging
import logging.handlers
#import cPickle as pickle
def test_StringReceivedIsSameAsStringSent():
host = 'localhost'
port = 9000
stringSent = "hello world!" 
stringReceived = None
log_msg = None
def sendLogToSocket(host,port, stringSent):
logger = logging.getLogger('mylogger') # to log Led Observer output over a socket
sh = logging.handlers.SocketHandler(host,port) # handler to write to socket
logger.addHandler(sh)
logger.critical(stringSent) 
logger.removeHandler(sh)
sh.close()
import threading
t = threading.Thread(target=sendLogToSocket, args=(host,port,stringSent)) # socket requires 2 different ports if on the same machine
t.start() # send log in a thread
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #INET => IPv4, STREAM => TCP
serversocket.bind((host,port)) # 'localhost' => implies only visible within the same machine
serversocket.listen(1) # accept 1 connection only
(clientsocket, address) = serversocket.accept()
stringReceived = clientsocket.recv(1024)
print 'socketlistener: string received: ', repr(stringReceived)
#obj = pickle.loads(stringReceived)
#print 'un pickling log: ', obj
#log_msg = logging.makeLogRecord(obj)
log_msg = logging.makeLogRecord(stringReceived)
print 'socketlistener: converted to log: ', log_msg
clientsocket.close()
serversocket.close()
t.join() # wait for the log thread to finish
print 'string sent: ', repr(stringSent), ' received: ', repr(stringReceived
self.assertEqual(stringSent, stringReceived)
if __name__ == "__main__":
test_StringReceivedIsSameAsStringSent()

输出

E:> python testSocket.py
socketlistener: string received:  'x00x00x01}}qx01(Ux0frelativeCreatedqx02 G@x18x00x0bx00x00x00x00Ux07processqx03Mxb0IUx04argsqx04NUx06moduleq x05UntestSocketqx06Ux08funcNameqx07Ux0fsendLogToSocketqx08Ux08exc_textq tNUx04nameqnUx08myloggerqx0bUx06threadqx0cM|"Ux07createdqrGAxd6ax040x b1xxd5UnthreadNameqx0eUx08Thread-1qx0fUx05msecsqx10G@x88(x00x01x00x0 0x00Ux08filenameqx11UrtestSocket.pyqx12Ux07levelnoqx13K2Ux0bprocessNameq x14Ux0bMainProcessqx15Ux08pathnameqx16hx12Ux06linenoqx17Kx12Ux03msgqx 18Ux0chello world!qx19Ux08exc_infoqx1aNUtlevelnameqx1bUx08CRITICALqx1cu.
'
Traceback (most recent call last):
File "testSocket.py", line 47, in <module>
test_StringReceivedIsSameAsStringSent()
File "testSocket.py", line 36, in test_StringReceivedIsSameAsStringSent
log_msg = logging.makeLogRecord(stringReceived)
File "C:UsersmyuserAppDataLocalContinuumMiniconda2liblogging__init_ _.py", line 340, in makeLogRecord
rv.__dict__.update(dict)
ValueError: dictionary update sequence element #0 has length 1; 2 is required

对不起,我的错! 忘记了 RTM !

需要 1. 解压缩大小和数据,2. 使用 size 解压缩数据,以及 3. makeLogRecord 未腌制的对象(之前错过的步骤 1(

感谢 python 文档上的代码:日志记录说明书示例和网络日志记录示例

这是我更正的代码(假设我们在一个套接字中得到大小和完整的消息.receive call(:

import unittest
import socketLogger
import logging
import logging.handlers
import pickle
#import cPickle as pickle
def test_StringReceivedIsSameAsStringSent():
host = 'localhost'
port = 9000
stringSent = "hello world!" 
stringReceived = None
log_msg = None
def sendLogToSocket(host,port, stringSent):
logger = logging.getLogger('mylogger') # to log Led Observer output over a socket
sh = logging.handlers.SocketHandler(host,port) # handler to write to socket
logger.addHandler(sh)
logger.critical(stringSent) 
logger.removeHandler(sh)
sh.close()
import threading
t = threading.Thread(target=sendLogToSocket, args=(host,port,stringSent)) # socket requires 2 different ports if on the same machine
t.start() # send log in a thread
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #INET => IPv4, STREAM => TCP
serversocket.bind((host,port)) # 'localhost' => implies only visible within the same machine
serversocket.listen(1) # accept 1 connection only
(clientsocket, address) = serversocket.accept()
chunk = clientsocket.recv(1024)
print 'socketlistener: data received: ', repr(chunk)
import struct
slen = struct.unpack(">L", chunk[:4])[0]
obj = pickle.loads(chunk[4:])
print 'un pickling log: ', repr(obj)
stringReceived = logging.makeLogRecord(obj)
#log_msg = logging.makeLogRecord(stringReceived)
print 'socketlistener: converted to log: ', repr(stringReceived)
clientsocket.close()
serversocket.close()
t.join() # wait for the log thread to finish
print 'string sent: ', repr(stringSent), ' received: ', repr(stringReceived.getMessage())
assert(stringSent == stringReceived.getMessage())
if __name__ == "__main__":
test_StringReceivedIsSameAsStringSent()

输出

E:> python testSocket.py
socketlistener: data received:  'x00x00x01}}qx01(Ux0frelativeCreatedqx02G@ !xffxe9x00x00x00x00Ux07processqx03M@5Ux04argsqx04NUx06moduleqx05Unt estSocketqx06Ux08funcNameqx07Ux0fsendLogToSocketqx08Ux08exc_textqtNUx04n ameqnUx08myloggerqx0bUx06threadqx0cM\JUx07createdqrGAxd6ax05x87xbfl x8bUnthreadNameqx0eUx08Thread-1qx0fUx05msecsqx10G@x8exf7xffxdfx00x00 x00Ux08filenameqx11UrtestSocket.pyqx12Ux07levelnoqx13K2Ux0bprocessNameq x14Ux0bMainProcessqx15Ux08pathnameqx16hx12Ux06linenoqx17Kx13Ux03msgqx1 8Ux0chello world!qx19Ux08exc_infoqx1aNUtlevelnameqx1bUx08CRITICALqx1cu.'
un pickling log:  {'threadName': 'Thread-1', 'name': 'mylogger', 'thread': 19036 , 'relativeCreated': 8.999824523925781, 'process': 13632, 'args': None, 'module' : 'testSocket', 'funcName': 'sendLogToSocket', 'levelno': 50, 'processName': 'Ma inProcess', 'created': 1501828638.991, 'msecs': 990.9999370574951, 'msg': 'hello world!', 'exc_info': None, 'exc_text': None, 'pathname': 'testSocket.py', 'file name': 'testSocket.py', 'levelname': 'CRITICAL', 'lineno': 19}
socketlistener: converted to log:  <logging.LogRecord object at 0x000000000276F4
00>
string sent:  'hello world!'  received:  'hello world!'

最新更新