Skip to main content

Nonblocking Logging Handler for Python Logging

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nonblockingloghandler-1.1.2a1.zip (37.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

nonblockingloghandler-1.1.2a1.win32.zip (13.8 kB view details)

Uploaded Source

nonblockingloghandler-1.1.2a1.win32.exe (208.6 kB view details)

Uploaded Source

nonblockingloghandler-1.1.2a1-py2.7.egg (13.0 kB view details)

Uploaded Egg

File details

Details for the file nonblockingloghandler-1.1.2a1.zip.

File metadata

File hashes

Hashes for nonblockingloghandler-1.1.2a1.zip
Algorithm Hash digest
SHA256 c6efcb26379844850c6d01af9c61c5ba0170de3327b966596a00955445ec7bd6
MD5 4c76e9dc823f7284ff0d40948ce07d45
BLAKE2b-256 baa60f450f3251eeb81087e07f2c34fa5c873edcaf6d1e9191e8459ad705f281

See more details on using hashes here.

File details

Details for the file nonblockingloghandler-1.1.2a1.win32.zip.

File metadata

File hashes

Hashes for nonblockingloghandler-1.1.2a1.win32.zip
Algorithm Hash digest
SHA256 227fa723a43c46a64c7441facf1f6f1dacd633f13b4415213362d7ee0607e86f
MD5 58f8a85087182417d0c0e6836056a1fb
BLAKE2b-256 5c10c74c8cacc6e49128c53e59945048e23a214a7c7469984d98801b84138b61

See more details on using hashes here.

File details

Details for the file nonblockingloghandler-1.1.2a1.win32.exe.

File metadata

File hashes

Hashes for nonblockingloghandler-1.1.2a1.win32.exe
Algorithm Hash digest
SHA256 66c51f715dd6b4a0137aebfba7b7ba93d6aae6974767bfa825285323cd964a79
MD5 b2964a38f64df4e20d79f208db576392
BLAKE2b-256 c2131f8fa2a26e6bf90648f83accf1f0e3b012261d620d10f2d170e3e4da1670

See more details on using hashes here.

File details

Details for the file nonblockingloghandler-1.1.2a1-py2.7.egg.

File metadata

File hashes

Hashes for nonblockingloghandler-1.1.2a1-py2.7.egg
Algorithm Hash digest
SHA256 4f1842a8e53390b3f68ed773db2702a45d952730d317450b537d7cad76641465
MD5 3565c598ac7653c42f03dd7e67c16b3f
BLAKE2b-256 2e9bc63c23667a19499647c666f6b3eefae7ac176f863ca14751e00059ebf968

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page