gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:46 24 4
gpt4 key购买 nike

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

无法将接收到的数据转换为日志记录。
按照 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 G@\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\x10G@\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

最佳答案

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

需要 1. 解压大小和数据,2. 使用大小解包数据,3. 解包对象的 makeLogRecord(之前错过了步骤 1)

感谢 python 文档上的代码:logging cookbook samplenetwork logging sample

这是我更正后的代码(假设我们在一个 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\x02G@ !\xff\xe9\x00\x00\x00\x00U\x07processq\x03M@5U\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\x10G@\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!'

关于python - 如何通过套接字检索 python.logging 日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45498916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com