Configuring Libraries

Configuring Libraries

Configuring Libraries

When developing libraries, you’ll probably need to use the NullHandler class.

N.B. This is part of the standard library since Python 2.7 / 3.1, so the version here is for use with earlier Python versions.

Typical usage:

import logging
try:
    from logging import NullHandler
except ImportError:
    from logutils import NullHandler

 # use this in all your library's subpackages/submodules
logger = logging.getLogger(__name__)

# use this just in your library's top-level package
logger.addHandler(NullHandler())
class logutils.NullHandler(level=0)

This handler does nothing. It’s intended to be used to avoid the “No handlers could be found for logger XXX” one-off warning. This is important for library code, which may contain code to log events. If a user of the library does not configure logging, the one-off warning might be produced; to avoid this, the library developer simply needs to instantiate a NullHandler and add it to the top-level logger of the library module or package.

createLock()

Since this handler does nothing, it has no underlying I/O to protect against multi-threaded access, so this method returns None.

emit(record)

Emit a record. This does nothing and shouldn’t be called during normal processing, unless you redefine handle().

handle(record)

Handle a record. Does nothing in this class, but in other handlers it typically filters and then emits the record in a thread-safe way.