liveBook · Manning

Stream handler

As explained in Python How-To

A stream handler is a logging component that sends log records to a stream such as a console or file-like object.

A stream handler sends log records to an interactive console, giving developers immediate feedback while an application runs. Create one with logging.StreamHandler() and attach it to a logger with addHandler(). A logger can use a stream handler alongside one or more file handlers, allowing real-time viewing and longer-term storage of the same event. section 12.1.3 section 12.1.4

Show a warning in the console

stream_handler = logging.StreamHandler() logger.addHandler(stream_handler) logger.warning("Just a random warning event.") # output: Just a random warning event.

Configure the handler with a logging level and Formatter when readable records matter. Include the event time, severity, module name, and message so that errors are easier to locate and correlate with outside events. The Formatter uses %-style fields rather than f-strings. section 12.2.3

How Python How-To covers it

How deep it goes
Explains construction, logger attachment, levels, and formatting, while only naming specialized handlers without detailing their implementation.
Examples
Uses a console warning and formatted application records containing timestamps, severity, module names, and messages.
What it assumes
Assumes a logger and basic logging configuration, while demonstrating stream-handler setup directly.
What you can do after
Apply it via Python's logging module to display and format application records in a console.
How it connects
Connects stream handlers with loggers, file handlers, logging levels, formatters, and log records.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.