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 alexber.utils.stdlogging module provides a StreamToLogger adapter and the initStream() helper that replaces a stream (typically sys.stderr or sys.stdout) with a logger-backed object. Every line written to the stream is forwarded to a Python logging.Logger. This is particularly useful in Docker containers and other environments where application logs must be captured from a single stream.

initStream()

The preferred API for redirecting a stream to a logger.
import logging
from alexber.utils.stdlogging import initStream

log = logging.getLogger('stderr')
initStream(logger=log, logger_level=logging.ERROR)

# sys.stderr is now backed by the logger
import sys
print('something went wrong', file=sys.stderr)  # routed to log.error(...)

Parameters

ParameterTypeDefaultDescription
loggerlogging.Loggerlogging.getLogger('stderr')Logger that receives the stream output.
logger_levelint or strlogging.ERRORLevel at which lines are logged. Accepts integer constants (logging.INFO) or strings ("INFO").
stream_gettercallableReturns sys.stderrSupplier that returns the stream to be wrapped.
stream_settercallableSets sys.stderrConsumer that installs the wrapped stream.
adapter_clstype or strStreamToLoggerAdapter class to use. Can be a class or a dotted import string.
As of v0.13.11, logger_level accepts both string level names ("INFO", "DEBUG") and integer constants (logging.INFO). String values are normalised to uppercase before lookup.

StreamToLogger

The adapter class that implements the stream interface and forwards writes to a logger. You generally do not need to instantiate this directly — use initStream() instead.
from alexber.utils.stdlogging import StreamToLogger
import logging
import sys

log = logging.getLogger('stdout')
sl = StreamToLogger(logger=log, stream=sys.stdout, logger_level=logging.INFO)
sys.stdout = sl

print('hello')  # calls log.info('hello')

Constructor parameters

ParameterRequiredDescription
loggerYesA logging.Logger or compatible logger object.
streamYesThe original stream being wrapped (sys.stderr, sys.stdout, etc.). Used only for flush().
logger_level / log_levelNoLog level. Defaults to logging.DEBUG.

Methods

  • write(lines) — Splits lines on newlines and logs each non-empty line at log_level.
  • flush() — Delegates to the original stream’s flush() method.

Redirecting stderr in Docker

In a Docker container, application logs are typically collected from the container’s stdout/stderr streams. If your application uses Python’s logging module with a file handler or a structured formatter, those logs never reach Docker’s log collector. Redirecting sys.stderr to a logger bridges the gap.
1

Configure the logging system

import logging

logging.basicConfig(
    level=logging.DEBUG,
    format='%(asctime)s %(name)s %(levelname)s %(message)s',
)
2

Redirect stderr to the logger

from alexber.utils.stdlogging import initStream
import logging

initStream(
    logger=logging.getLogger('stderr'),
    logger_level=logging.ERROR,
)
After this call, anything written to sys.stderr — including unhandled exception tracebacks — is logged through the configured logging system.
3

Optionally redirect stdout as well

import sys

initStream(
    logger=logging.getLogger('stdout'),
    logger_level=logging.INFO,
    stream_getter=lambda: sys.stdout,
    stream_setter=lambda s: setattr(sys, 'stdout', s),
)
Redirecting sys.stdout is fragile. Some C extensions and third-party libraries write directly to the underlying file descriptor and will bypass the logger. Redirect sys.stderr first; only redirect sys.stdout if you have a specific need.

Using a string log level

from alexber.utils.stdlogging import initStream

# String levels are accepted since v0.13.11
initStream(logger_level='WARNING')
initStream(logger_level='debug')   # case-insensitive

Build docs developers (and LLMs) love