13 Subclassing QueueHandler - a ZeroMQ example You can use a QueueHandler subclass to send messages to other kinds of queues, for example a ZeroMQ ‘publish’ socket. In the example below,the socket is created separately and passed to the handler (as its ‘queue’): import zmq # using pyzmq, the Python binding for ZeroMQ import json # for serializing records portably ctx =zmq.Context() sock =zmq.Socket(ctx, zmq .PUB) # or zmq.PUSH, or other suitable value sock.bind('tcp://*:5556 ') # or wherever classZeroMQSocketHandler (QueueHandler): def enqueue(self, record): self.queue.send_json(record .__dict__ ) handler =ZeroMQSocketHandler(sock) classZeroMQSocketHandler (QueueHandler): def __init__ (self, uri, socktype =zmq.PUB, ctx= None ): self.ctx =ctx or zmq.Context() socket =zmq.Socket(self.ctx, socktype) socket.bind(uri) super() .__init__ (socket) def enqueue(self, record): self.queue.send_json(record .__dict__ ) def close(self): self.queue.close() Of course there are other ways of organizing this, for example passing in the data needed by the handler to create the socket: You can also subclass$_{QueueListener}$ to get messages from other kinds of queues, for example a ZeroMQ ‘sub- scribe’ socket. Here’s an example: 14 Subclassing QueueListener - a ZeroMQ example classZeroMQSocketListener (QueueListener): def __init__ (self, uri,/,*handlers,**kwargs): self.ctx =kwargs.get('ctx') or zmq.Context() socket =zmq.Socket(self.ctx, zmq .SUB) socket.setsockopt_string(zmq .SUBSCRIBE, '') # subscribe to everything socket.connect(uri) super() .__init__ (socket,*handlers,**kwargs) def dequeue(self): msg =self.queue.recv_json() return logging.makeLogRecord(msg) See also: Module$_{logging}$ API reference for the logging module. Module$_{logging.config}$ Configuration API for the logging module. Module$_{logging.handlers}$ Useful handlers included with the logging module. 22