On this page
logging.StreamHandler
Prev: logging.shutdown | Up: logging | Next: logging.warn
Rel: logging.addLevelName, logging.basicConfig, logging.BufferingFormatter, logging.captureWarnings
Ref: docs.python.org — logging.StreamHandler
Tags: #public #python3 #stdlib #logging
Import
import logging
from logging import StreamHandler
Signature & Overview
logging.StreamHandler(stream=None)
A handler class which writes logging records, appropriately formatted, to a stream. Note that this class does not close the stream, as sys.stdout or sys.stderr may be used.
Methods & Attributes
StreamHandler.acquire(self)— Acquire the I/O thread lock.StreamHandler.addFilter(self, filter)— Add the specified filter to this handler.StreamHandler.close(self)— Tidy up any resources used by the handler.StreamHandler.createLock(self)— Acquire a thread lock for serializing access to the underlying I/O.StreamHandler.emit(self, record)— Emit a record.StreamHandler.filter(self, record)— Determine if a record is loggable by consulting all the filters.StreamHandler.flush(self)— Flushes the stream.StreamHandler.format(self, record)— Format the specified record.StreamHandler.get_name(self)—StreamHandler.get_namemethod/property.StreamHandler.handle(self, record)— Conditionally emit the specified logging record.StreamHandler.handleError(self, record)— Handle errors which occur during an emit() call.StreamHandler.name(...)—StreamHandler.namemethod/property.StreamHandler.release(self)— Release the I/O thread lock.StreamHandler.removeFilter(self, filter)— Remove the specified filter from this handler.StreamHandler.setFormatter(self, fmt)— Set the formatter for this handler.StreamHandler.setLevel(self, level)— Set the logging level of this handler. level must be an int or a str.StreamHandler.setStream(self, stream)— Sets the StreamHandler's stream to the specified value, if it is different.StreamHandler.set_name(self, name)—StreamHandler.set_namemethod/property.
Usage
import io
import logging
buf = io.StringIO()
h = logging.StreamHandler(buf)
h.setFormatter(logging.Formatter("%(levelname)s:%(message)s"))
rec = logging.LogRecord("cli", logging.INFO, "", 1, "ready", (), None)
h.emit(rec)
assert buf.getvalue().strip() == "INFO:ready"
Gotchas
- Defaults to
sys.stderr, notsys.stdout: Passstream=sys.stdoutexplicitly if your CLI expects logs on stdout.
REPL Inspect
from logging import StreamHandler
help(StreamHandler)
Linked from
Related notes
- tuts/python3/stdlib_module/logging/shutdown Linked here; links to this page; shared tags: #logging, #stdlib
- tuts/python3/stdlib_module/logging/warn 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