Skip to main content
Nonblocking Log Handler
-----------------------

Provides a NonblockingLogHandler class consistent with the Python logging
subsystem.

This handler acts as a proxy for the another log handler that may be slow
to execute: e.g. the SMTPHandler, SocketHandler, SysLogHandler - especially
when they are talking to remote servers.

If you have a real-time system (i.e. one where a late response is a wrong
response) 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 your
proxied handler. Unlike the proxied handler, this handler which returns
quickly, and executes the actually logging in the background, in a separate
thread.

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 the
scope of this module.

Execution of str() functions on message parameters and string formatting
is done immediately, in the calling thread. This is to guarantee both
the thread-safety of the logged objects and also that the log shows the
object'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 user
parameters from the logging call passed to it. It will only receive the
resulting message string formatted according to the client's wishes. It
can still format dates, threadnames, levels, etc.

* If you subclass from Filter, be aware that they should be fast. I/O-bound
filters are outside of the scope of this module.

* Clients should not directly call format() or emit() on the
NonblockingLogHandler class. Consider them private.

* Once the NonblockingLogHandler handler is initialised, any further calls
to setLevel(), addFilter and removeFilter() made on the delegated handler
are ignored by the proxy handler. Conversely, the delegated handler is
not 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 using
it elsewhere in the application. This makes it difficult to ever call
close() 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 Windows
only.
* Results in exception report being displayed to screen:
"Exception in thread NonblockingLogHandler.[...] (most likely raised
during interpreter shutdown)"
* Suspected (unconfirmed) cause Python run-time bug, fixed in Python 3.2.

Metadata

Release files for nonblockingloghandler 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nonblockingloghandler 1.1.2
File Size Uploaded
nonblockingloghandler-1.1.2.zip 37.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nonblockingloghandler 1.1.2
File Interpreter ABI Platform
nonblockingloghandler-1.1.2-py2.7.egg Legacy Egg format - - Details

Total release size: 50.3 kB

Release files / nonblockingloghandler-1.1.2.zip

Download URL nonblockingloghandler-1.1.2.zip
Size 37.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7d7a4ff36143e9857b572fe1791b02fe143f2d0eec70ff5139a2330ebfa5ac72
BLAKE2b-256 checksum
How to use checksums
725cd8333dfb9995ef7f99436724f65235681a85944c68410165301e15bc577f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / nonblockingloghandler-1.1.2-py2.7.egg

Download URL nonblockingloghandler-1.1.2-py2.7.egg
Size 13.0 kB
Tags Egg
SHA-256 checksum
How to use checksums
144fa2385f2b93cecf892ea8e8429bdc16096172edd8adb12bd879bdee64b8e0
BLAKE2b-256 checksum
How to use checksums
533c533eebdebbbb3e0df45f06bb8bb679cfb894f6a9992f56dc15fae8209387
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page