$_{LogRecords}$, for that matter). The LogRecords are removed from the queue and passed to the handlers for processing. 

The advantage of having a separate$_{QueueListener}$ class is that you can use the same instance to service multiple QueueHandlers . This is more resource-friendly than, say, having threaded versions of the existing handler classes, which would eat up one thread per handler for no particular benefit. 

An example of using these two classes follows (imports omitted): 

que =queue.Queue(-1) # no limit on size queue_handler=QueueHandler(que) handler =logging.StreamHandler() listener =QueueListener(que, handler) root =logging.getLogger() root.addHandler(queue_handler) formatter =logging.Formatter(' %(threadName)s : %(message)s ') handler.setFormatter(formatter) listener.start() # The log output will display the thread which generated # the event (the main thread) rather than the internal # thread which monitors the internal queue. This is what # you want to happen. root.warning( 'Look out!') listener.stop() 

which, when run, will produce: 

MainThread: Look out! 

Changed in version 3.5: Prior to Python 3.5, the QueueListener always passed every message received from the queue to every handler it was initialized with. (This was because it was assumed that level filtering was all done on the other side, where the queue is filled.) From 3.5 onwards, this behaviour can be changed by passing a keyword argument respect_handler_level=True to the listener’s constructor. When this is done, the listener compares the level of each message with the handler’s level, and only passes a message to a handler if it’s appropriate to do so. 

## 7 Sending and receiving logging events across a network 

Let’s say you want to send logging events across a network, and handle them at the receiving end. A simple way of doing this is attaching a$_{SocketHandler}$instance to the root logger at the sending end: 

import logging , logging.handlers rootLogger =logging.getLogger('') rootLogger.setLevel(logging .DEBUG) socketHandler =logging.handlers.SocketHandler( 'localhost', logging.handlers.DEFAULT_TCP_LOGGING_PORT) # don't bother with a formatter, since a socket handler sends the event as # an unformatted pickle rootLogger.addHandler(socketHandler) # Now, we can log to the root logger, or any other logger. First the root... logging.info('Jackdaws love my big sphinx of quartz. ') # Now, define a couple of other loggers which might represent areas in your # application: logger1=logging.getLogger('myapp.area1 ') logger2=logging.getLogger('myapp.area2 ') 

(continues on next page) 

