2017-08-04 30 views
0

作为我的测试的一部分,试图检查套接字是否收到日志。
所以一个线程发送日志,主线程尝试从套接字中检索。如何通过套接字检索python.logging日志记录

无法将接收到的数据转换为日志记录。
按照Python 2.7 docs试验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: '\x00\x00\x01}}q\x01(U\x0frelativeCreatedq\x02 [email protected]\x18\x00\x0b\x00\x00\x00\x00U\x07processq\x03M\xb0IU\x04argsq\x04NU\x06moduleq \x05U\ntestSocketq\x06U\x08funcNameq\x07U\x0fsendLogToSocketq\x08U\x08exc_textq\ tNU\x04nameq\nU\x08myloggerq\x0bU\x06threadq\x0cM|"U\x07createdq\rGA\xd6a\x040\x b1x\xd5U\nthreadNameq\x0eU\x08Thread-1q\x0fU\x05msecsq\[email protected]\x88(\x00\x01\x00\x0 0\x00U\x08filenameq\x11U\rtestSocket.pyq\x12U\x07levelnoq\x13K2U\x0bprocessNameq \x14U\x0bMainProcessq\x15U\x08pathnameq\x16h\x12U\x06linenoq\x17K\x12U\x03msgq\x 18U\x0chello world!q\x19U\x08exc_infoq\x1aNU\tlevelnameq\x1bU\x08CRITICALq\x1cu. 
' 
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:\Users\myuser\AppData\Local\Continuum\Miniconda2\lib\logging\__init_ _.py", line 340, in makeLogRecord 
    rv.__dict__.update(dict) 
ValueError: dictionary update sequence element #0 has length 1; 2 is required 
+0

请使用'print repr(string_variable)'而不是'print' –

+0

@ArindamRoychowdhury在这里你去。 – vijiboy

回答

0

对不起我的坏!忘了RTM! logging cookbook samplenetwork logging sample

要求1使用尺寸以及3.拆封对象(较早错过步骤1)

由于代码上python文档的makeLogRecord解包大小和数据,2. unpickle数据

这里是我的代码修正(假设我们得到一个socket.receive呼叫规模和完整的消息):

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: '\x00\x00\x01}}q\x01(U\x0frelativeCreatedq\[email protected] !\xff\xe9\x00\x00\x00\x00U\x07processq\[email protected]\x04argsq\x04NU\x06moduleq\x05U\nt estSocketq\x06U\x08funcNameq\x07U\x0fsendLogToSocketq\x08U\x08exc_textq\tNU\x04n ameq\nU\x08myloggerq\x0bU\x06threadq\x0cM\\JU\x07createdq\rGA\xd6a\x05\x87\xbfl\ x8bU\nthreadNameq\x0eU\x08Thread-1q\x0fU\x05msecsq\[email protected]\x8e\xf7\xff\xdf\x00\x00 \x00U\x08filenameq\x11U\rtestSocket.pyq\x12U\x07levelnoq\x13K2U\x0bprocessNameq\ x14U\x0bMainProcessq\x15U\x08pathnameq\x16h\x12U\x06linenoq\x17K\x13U\x03msgq\x1 8U\x0chello world!q\x19U\x08exc_infoq\x1aNU\tlevelnameq\x1bU\x08CRITICALq\x1cu.' 

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!'