Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 63891ed

Browse files
committed
Expanded QueueHandler documentation.
1 parent ccb706c commit 63891ed

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

Doc/library/logging.rst

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2605,7 +2605,9 @@ supports sending logging messages to a queue, such as those implemented in the
26052605
.. class:: QueueHandler(queue)
26062606

26072607
Returns a new instance of the :class:`QueueHandler` class. The instance is
2608-
initialized with the queue to send messages to.
2608+
initialized with the queue to send messages to. The queue can be any queue-
2609+
like object; it's passed as-is to the :meth:`enqueue` method, which needs
2610+
to know how to send messages to it.
26092611

26102612

26112613
.. method:: emit(record)
@@ -2623,6 +2625,37 @@ supports sending logging messages to a queue, such as those implemented in the
26232625

26242626
The :class:`QueueHandler` class was not present in previous versions.
26252627

2628+
.. _zeromq-handlers:
2629+
2630+
You can use a :class:`QueueHandler` subclass to send messages to other kinds
2631+
of queues, for example a ZeroMQ "publish" socket. In the example below,the
2632+
socket is created separately and passed to the handler (as its 'queue')::
2633+
2634+
import zmq # using pyzmq, the Python binding for ZeroMQ
2635+
import json # for serializing records portably
2636+
2637+
ctx = zmq.Context()
2638+
sock = zmq.Socket(ctx, zmq.PUB) # or zmq.PUSH, or other suitable value
2639+
sock.bind('tcp://*:5556') # or wherever
2640+
2641+
class ZeroMQSocketHandler(QueueHandler):
2642+
def enqueue(self, record):
2643+
data = json.dumps(record.__dict__)
2644+
self.queue.send(data)
2645+
2646+
Of course there are other ways of organizing this, for example passing in the
2647+
data needed by the handler to create the socket::
2648+
2649+
class ZeroMQSocketHandler(QueueHandler):
2650+
def __init__(self, uri, socktype=zmq.PUB, ctx=None):
2651+
self.ctx = ctx or zmq.Context()
2652+
socket = zmq.Socket(self.ctx, socktype)
2653+
super(ZeroMQSocketHandler, self).__init__(socket)
2654+
2655+
def enqueue(self, record):
2656+
data = json.dumps(record.__dict__)
2657+
self.queue.send(data)
2658+
26262659

26272660
.. _formatter-objects:
26282661

0 commit comments

Comments
 (0)