Package: nonblockingloghandler
Nonblocking Log Handler-----------------------Provides a NonblockingLogHandler class consistent with the Python loggingsubsystem.This handler acts as a proxy for the another log handler that may be slowto execute: e.g. the SMTPHandler, SocketHandler, SysLogHandler - especiallywhen they are talking to remote servers.If you have a real-time system (i.e. one where a late response is a wrongresponse) and you are sending log messages via email, http, syslog, etc.,you should consider using this module to protect against unexpected delays.It is intended to be a drop-in replacement (see provisos below) for yourproxied handler. Unlike the proxied handler, this handler which returnsquickly, and executes the actually logging in the background, in a separatethread.Provisos:* If you pass an objects to the logging functions, be aware that their__str__() functions should be fast. I/O bound str() calls are outside thescope of this module.Execution of str() functions on message parameters and string formattingis done immediately, in the calling thread. This is to guarantee boththe thread-safety of the logged objects and also that the log shows theobject's values at the time of the call, not the time of the emit.* If you subclass from Formatter, note that it will not have the userparameters from the logging call passed to it. It will only receive theresulting message string formatted according to the client's wishes. Itcan still format dates, threadnames, levels, etc.* If you subclass from Filter, be aware that they should be fast. I/O-boundfilters are outside of the scope of this module.* Clients should not directly call format() or emit() on theNonblockingLogHandler class. Consider them private.* Once the NonblockingLogHandler handler is initialised, any further callsto setLevel(), addFilter and removeFilter() made on the delegated handlerare ignored by the proxy handler. Conversely, the delegated handler isnot informed of calls made on the NonblockingLogHandler instance.Best practice would be to only update the NonblockingLogHandler handler,and not use the values stored in the delegated handler.* If the close() method is called on the NonblockingLogHandler handler,it does *not* call close on the delegated handler, in case you are usingit elsewhere in the application. This makes it difficult to ever callclose() on the delegated handler, becausse it may still be in use.Example usage:nonblocking_email_handler = nonblockingloghandler.NonblockingLogHandler(logging.SMTPHandler("localhost", "logging_system@example.com","admin@example.com", "Log message"))db_logger = logging.getLogger("database")database_logger.addHandler(nonblocking_email_handler)# This operation will return immediately, before email is sent.db_logger.critical("Database corrupted")nonblocking_email_handler.close()Limitations:* Only tested on Python 2.7.2, Windows and Ubuntu* Let me know if there is demand for other versions.* Known to have a rare intermittent shut-down race condition on Windowsonly.* Results in exception report being displayed to screen:"Exception in thread NonblockingLogHandler.[...] (most likely raisedduring interpreter shutdown)"* Suspected (unconfirmed) cause Python run-time bug, fixed in Python 3.2.