Send Python logs to standard output
Route Python logging to stdout with basicConfig or StreamHandler, and understand configuration ownership and container log collection.
Pass sys.stdout to the logging stream handler. A default StreamHandler, including the one created by a plain basicConfig(), writes to stderr instead.
Configure a simple application
import logging
import sys
logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logging.info("This record goes to stdout")
Call this before other application logging configuration. basicConfig() normally does nothing if the root logger already has handlers. In an application where you intentionally own and replace the root configuration, force=True on Python 3.8+ removes and closes those existing handlers first. Libraries should not force a caller's configuration to change.
Configure a dedicated logger
import logging
import sys
logger = logging.getLogger("my_app")
logger.setLevel(logging.INFO)
logger.propagate = False
handler = logging.StreamHandler(sys.stdout)
handler.setFormatter(logging.Formatter(
"%(asctime)s [%(levelname)s] %(message)s"
))
logger.addHandler(handler)
logger.info("Hello, stdout!")
Run this setup once. With propagate=False, this logger's records stay with its own handler instead of also reaching a root handler. If your application centrally configures root logging, a module logger with normal propagation may be the better design.
Container collection
Writing application logs to stdout or stderr lets the container runtime and platform logging stack collect the process streams. This avoids relying on an unmounted file inside a container whose lifecycle may be short. File logging can still be appropriate with deliberate persistent storage and collection configuration.
Docker supports multiple logging drivers; do not assume every deployment uses json-file. Kubernetes commonly uses a container runtime's standard-stream logs, with a separate collector for centralized retention. Verify your runtime, logging driver, and collector configuration rather than assuming stdout alone sends records to a remote platform.
FAQ
Should errors go to stderr? Both streams can carry logs. Some systems distinguish them operationally, while others rely on a structured severity field. The handler above sends every accepted severity to stdout; splitting by severity requires additional handlers and filters.
How does logging differ from print? Logging adds levels, routing, timestamps, and context through handlers and formatters. print remains useful for a program's normal textual output; keep that output separate from diagnostics when another program consumes it.
Why do container logs arrive late? Standard StreamHandler flushes after emitting each record. python -u or PYTHONUNBUFFERED=1 can help buffered print output, but will not fix batching in a queue handler, runtime log driver, collector, or network. Check which stage is introducing the delay.
References
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan