On this page
logging.Handler
Prev: logging.getLogRecordFactory | Up: logging | Next: logging.info
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.Handler
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import Handler
Signature & Overview
logging.Handler(level=0)
Handler instances dispatch logging events to specific destinations.
Methods & Attributes
Handler.acquire(self)— Acquire the I/O thread lock.Handler.addFilter(self, filter)— Add the specified filter to this handler.Handler.close(self)— Tidy up any resources used by the handler.Handler.createLock(self)— Acquire a thread lock for serializing access to the underlying I/O.Handler.emit(self, record)— Do whatever it takes to actually log the specified logging record.Handler.filter(self, record)— Determine if a record is loggable by consulting all the filters.Handler.flush(self)— Ensure all logging output has been flushed.Handler.format(self, record)— Format the specified record.Handler.get_name(self)—Handler.get_namemethod/property.Handler.handle(self, record)— Conditionally emit the specified logging record.Handler.handleError(self, record)— Handle errors which occur during an emit() call.Handler.name(...)—Handler.namemethod/property.Handler.release(self)— Release the I/O thread lock.Handler.removeFilter(self, filter)— Remove the specified filter from this handler.Handler.setFormatter(self, fmt)— Set the formatter for this handler.Handler.setLevel(self, level)— Set the logging level of this handler. level must be an int or a str.Handler.set_name(self, name)—Handler.set_namemethod/property.
Usage
import logging
class ListHandler(logging.Handler):
def __init__(self) -> None:
super().__init__()
self.messages: list[str] = []
def emit(self, record: logging.LogRecord) -> None:
self.messages.append(self.format(record))
h = ListHandler()
h.handle(logging.LogRecord("t", logging.INFO, "", 1, "ping", (), None))
assert h.messages == ["ping"]
Gotchas
- Abstract base class: Subclass
Handlerand implementemit(self, record)to ship logs to a custom sink (in-memory test list, queue, or HTTP endpoint).
REPL Inspect
from logging import Handler
help(Handler)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/getLogRecordFactory Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/info 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