How to use msgpack instead of pickle for logging SocketHandler in Python? -


the logging cookbook describes method using python logging functionality remote log server.

unfortunately, implementation requires exposing of server, process received data unpickle function, severe security risk.

i'd use msgpack library pass logging message instead of pickle module. there ready use solution based on logging , msgpack available? there special precautions consider, when implementing myself?

update have managed modify original example logging cookbook, , obtained seems work.

the log server:

import logging import logging.handlers import socketserver import struct import msgpack  class logrecordstreamhandler(socketserver.streamrequesthandler):     """handler streaming logging request.      logs record using whatever logging policy     configured locally.     """      def handle(self):         """         handle multiple requests - each expected 4-byte length,         followed logrecord in pickle format. logs record         according whatever policy configured locally.         """         import msgpack p         unp=p.unpacker()         while true:             r=self.request.recv(1000)             if len(r)==0:                 break             unp.feed(r)             obj in unp:                 record = logging.makelogrecord(obj)                 self.handlelogrecord(record)       def handlelogrecord(self, record):         # if name specified, use named logger rather 1         # implied record.         if self.server.logname not none:             name = self.server.logname         else:             name = record.name         logger = logging.getlogger(name)         # n.b. every record gets logged. because logger.handle         # called after logger-level filtering. if want         # filtering, @ client end save wasting         # cycles , network bandwidth!         logger.handle(record)  class logrecordsocketreceiver(socketserver.threadingtcpserver):     """     simple tcp socket-based logging receiver suitable testing.     """      allow_reuse_address = 1      def __init__(self, host='localhost',                  port=65432,                  handler=logrecordstreamhandler):         socketserver.threadingtcpserver.__init__(self, (host, port), handler)         self.abort = 0         self.timeout = 1         self.logname = none      def serve_until_stopped(self):         import select         abort = 0         while not abort:             rd, wr, ex = select.select([self.socket.fileno()],                                        [], [],                                        self.timeout)             if rd:                 self.handle_request()             abort = self.abort  def main():     logging.basicconfig(         format='%(relativecreated)5d %(name)-15s %(levelname)-8s %(message)s')     tcpserver = logrecordsocketreceiver()     print('about start tcp server...')     tcpserver.serve_until_stopped()  if __name__ == '__main__':     main() 

sample client:

import logging, logging.handlers import msgpack import test1 #import auxiliary_module  class msgpackhandler(logging.handlers.sockethandler):   def __init__(self, host, port):      logging.handlers.sockethandler.__init__(self,host,port)   def makepickle(self,record):      #use msgpack instead of pickle, increased safety      return msgpack.packb(record.__dict__) # create logger 'spam_application' logger = logging.getlogger('my_log') logger.setlevel(logging.debug) # create file handler logs debug messages #fh = logging.filehandler('spam.log') fh=msgpackhandler('localhost',65432) fh.setlevel(logging.debug) # create formatter , add handler formatter = logging.formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setformatter(formatter) # add handlers logger logger.addhandler(fh)  logger.info('this sample info message') logger.error('this sample error message') 


Comments