Skip to content

Instantly share code, notes, and snippets.

@shivamMg
Created September 17, 2025 11:45
Show Gist options
  • Select an option

  • Save shivamMg/50f7a023d2be2df60185791d36a8ad48 to your computer and use it in GitHub Desktop.

Select an option

Save shivamMg/50f7a023d2be2df60185791d36a8ad48 to your computer and use it in GitHub Desktop.
Python: Log everything, without buffering
import os, sys, logging
# 1) Disable Python’s stdout buffering
os.environ["PYTHONUNBUFFERED"] = "1"
try:
# Python 3.7+ lets you force line-buffering on stdout
sys.stdout.reconfigure(line_buffering=True)
except Exception:
pass
# 2) Configure logging to stream to stdout and flush on every record
logging.basicConfig(
level=logging.INFO, # or DEBUG
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
handlers=[logging.StreamHandler(sys.stdout)],
force=True, # override any previous config
)
# 3) For any ad-hoc prints, force a flush
print("Logging initialized (unbuffered).", flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment