Created
September 17, 2025 11:45
-
-
Save shivamMg/50f7a023d2be2df60185791d36a8ad48 to your computer and use it in GitHub Desktop.
Python: Log everything, without buffering
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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