Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/alex-ber/AlexBerUtils/llms.txt

Use this file to discover all available pages before exploring further.

The stdlogging module provides a StreamToLogger adapter class and an initStream convenience function for redirecting any stream-like object (typically sys.stderr or sys.stdout) to a Python logging.Logger.
Capturing stdout can be fragile. Use with care and test thoroughly in your environment.

StreamToLogger

Adapter class that wraps a stream-like object and forwards each written line to a logging.Logger.
import sys
import logging
from alexber.utils.stdlogging import StreamToLogger

logging.basicConfig()
logger = logging.getLogger('stdout')

sl = StreamToLogger(logger=logger, stream=sys.stdout, log_level=logging.INFO)
sys.stdout = sl

print('This goes to the logger now')
It is recommended to use initStream() rather than instantiating StreamToLogger directly.

__init__

logger
logging.Logger
required
Standard Python Logger or any logger-compatible object. Cannot be None.
stream
IO
required
The original stream being wrapped (e.g. sys.stderr, sys.stdout). Cannot be None. Used by flush() to delegate flush calls.
log_level
int | str
Log level for emitted messages. Accepts an int constant (e.g. logging.DEBUG) or a level-name string (e.g. 'INFO'). Defaults to logging.DEBUG. The alias logger_level is also accepted.

write(lines)

Writes one or more lines to the logger. Each non-empty line is logged individually at the configured level.
lines
str
required
String content to log. Empty strings are silently ignored.

flush()

Delegates flush() to the underlying stream. Keeps the adapter compatible with the stream protocol.

initStream

Preferred API. Wraps a stream with StreamToLogger (or a custom adapter) and redirects it to a logger.
import logging
from alexber.utils.stdlogging import initStream

logging.basicConfig()

# Redirect sys.stderr to a logger named 'stderr' at ERROR level (defaults)
initStream()

# Redirect sys.stdout to a custom logger at INFO level
import sys
initStream(
    logger=logging.getLogger('stdout'),
    logger_level=logging.INFO,
    stream_getter=lambda: sys.stdout,
    stream_setter=lambda s: setattr(sys, 'stdout', s),
)
logger
logging.Logger
Logger to route stream output to. If not supplied, logging.getLogger('stderr') is used.
logger_level
int | str
Log level for emitted messages. Accepts an int constant or a level-name string. Defaults to logging.ERROR.
stream_getter
Callable[[], IO]
Zero-argument callable that returns the stream to wrap. If not supplied, defaults to lambda: sys.stderr.
stream_setter
Callable[[IO], None]
One-argument callable that installs the wrapped adapter in place of the original stream. If not supplied, defaults to lambda s: setattr(sys, 'stderr', s).
adapter_cls
type | str
Adapter class to instantiate. Can be a class or a fully-qualified dotted string resolved via importer. Defaults to StreamToLogger.
Returns: None.