(continued from previous page) 

allow_reuse_address = True def __init__ (self, host='localhost', port=logging.handlers.DEFAULT_TCP_LOGGING_PORT, 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 to start TCP server... ') tcpserver.serve_until_stopped() if __name__ =='__main__ ': main() 

First run the server, and then the client. On the client side, nothing is printed on the console; on the server side, you should see something like: 

About to start TCP server... 59 root INFO Jackdaws love my big sphinx of quartz. 59 myapp.area1 DEBUG Quick zephyrs blow, vexing daft Jim. 69 myapp.area1 INFO How quickly daft jumping zebras vex. 69 myapp.area2 WARNING Jail zesty vixen who grabbed pay from quack. 69 myapp.area2 ERROR The five boxing wizards jump quickly. 

Note that there are some security issues with pickle in some scenarios. If these affect you, you can use an alternative serialization scheme by overriding the$_{makePickle()}$ method and implementing your alternative there, as well as adapting the above script to use your alternative serialization. 

## 8 Adding contextual information to your logging output 

Sometimes you want logging output to contain contextual information in addition to the parameters passed to the logging call. For example, in a networked application, it may be desirable to log client-specific information in the log (e.g. remote client’s username, or IP address). Although you could use the extra parameter to achieve this, it’s not always convenient to pass the information in this way. While it might be tempting to create$_{Logger}$ instances on a per-connection basis, this is not a good idea because these instances are not garbage collected. While this is not a problem in practice, when the number of$_{Logger}$ instances is dependent on the level of granularity you want to use in logging an application, it could be hard to manage if the number of$_{Logger}$ instances becomes effectively unbounded. 

