On this page
logging.NullHandler
Prev: logging.makeLogRecord | Up: logging | Next: logging.setLoggerClass
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.NullHandler
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import NullHandler
Signature & Overview
logging.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
Methods & Attributes
NullHandler.acquire(self)— Acquire the I/O thread lock.NullHandler.addFilter(self, filter)— Add the specified filter to this handler.NullHandler.close(self)— Tidy up any resources used by the handler.NullHandler.createLock(self)—NullHandler.createLockmethod/property.NullHandler.emit(self, record)— Stub.NullHandler.filter(self, record)— Determine if a record is loggable by consulting all the filters.NullHandler.flush(self)— Ensure all logging output has been flushed.NullHandler.format(self, record)— Format the specified record.NullHandler.get_name(self)—NullHandler.get_namemethod/property.NullHandler.handle(self, record)— Stub.NullHandler.handleError(self, record)— Handle errors which occur during an emit() call.NullHandler.name(...)—NullHandler.namemethod/property.NullHandler.release(self)— Release the I/O thread lock.NullHandler.removeFilter(self, filter)— Remove the specified filter from this handler.NullHandler.setFormatter(self, fmt)— Set the formatter for this handler.NullHandler.setLevel(self, level)— Set the logging level of this handler. level must be an int or a str.NullHandler.set_name(self, name)—NullHandler.set_namemethod/property.
Usage
import logging
# Recommended setup in a reusable library's __init__.py
lib_logger = logging.getLogger("mylib")
lib_logger.addHandler(logging.NullHandler())
lib_logger.warning("silent unless the application configures logging")
Gotchas
- Always attach
NullHandler()in libraries: Prevents Python'slastResorthandler from printing unwantedWARNINGmessages when the consuming app hasn't configured logging.
REPL Inspect
from logging import NullHandler
help(NullHandler)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/makeLogRecord Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/setLoggerClass Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/logging/addLevelName Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/basicConfig Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/BufferingFormatter Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/captureWarnings Linked here; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/critical shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/debug shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/disable shared tags: #logging, #stdlib