How to turn off logging buffer to get logs in real time with python command line tool?

2024/9/18 21:58:29

I have a command line tool which produces plenty of logs. I want these logs to be sent to stdout as soon as they're made. Right now, the program finishes everything (which can take several minutes), and then sends everything to stdout all at once. This means that currently, the user is not aware of whats going on internally, and I want to change that.

So far I've tried passing the -u flag as a command line argument, but that doesn't change anything. I have a feeling I need to use logging.handlers.BufferingHandler with flush() in some capacity, but I haven't been able to figure it out through tinkering.

def some_func(flag: bool):if flag:logging.info('flag is truthy')returnlogging.warning('flag is falsy')

I'd like the above function to show the user one of those logs as soon as the log is created. Note: currently I set the logging level with logging.root.setLevel(logging.INFO).

Thanks!

Answer

you have to set environmental variable PYTHONUNBUFFERED=1

https://en.xdnf.cn/q/72604.html

Related Q&A

How to tell if process is responding in Python on Windows

I am writing a python script to keep a buggy program open and I need to figure out if the program is not respoding and close it on windows. I cant quite figure out how to do this.

How to load and use a pretained PyTorch InceptionV3 model to classify an image

I have the same problem as How can I load and use a PyTorch (.pth.tar) model which does not have an accepted answer or one I can figure out how to follow the advice given.Im new to PyTorch. I am trying…

Append list to pandas DataFrame as new row with index

Despite of the numerous stack overflow questions on appending data to a dataframe I could not really find an answer to the following. I am looking for a straight forward solution to append a list as la…

IPC between Python and C#

I want to pass data between a Python and a C# application in Windows (I want the channel to be bi-directional) In fact I wanna pass a struct containing data about a network packet that Ive captured wit…

Saving matplotlib subplot figure to image file

Im fairly new to matplotlib and am limping along. That said, I havent found an obvious answer to this question.I have a scatter plot I wanted colored by groups, and it looked like plotting via a loop w…

Numpy - Dot Product of a Vector of Matrices with a Vector of Scalars

I have a 3 dimensional data set that I am trying to manipulate in the following way. data.shape = (643, 2890, 10) vector.shape = (643,)I would like numpy to see data as a 643 length 1-D array of 2890x1…

delete node in binary search tree python

The code below is my implement for my binary search tree, and I want to implement delete method to remove the node. Below is my implementation, but when I perform bst = BSTRee() bst.insert(5) bst.inser…

ImportError: cannot import name cbook when using PyCharms Profiler

I am trying to run the PyCharm profiler but I get the following error message:Traceback (most recent call last):File "/home/b3053674/ProgramFiles/pycharm-2017.1.4/helpers/profiler/run_profiler.py&…

Replicating SAS first and last functionality with Python

I have recently migrated to Python as my primary tool for analysis and I am looking to be able to replicate the first. & last. functionality found in SAS. The SAS code would be as follows;data data…

Can mypy track string literals?

Is there anyway to make this work from typing import Literal def foo(bar: Literal["bar"]) -> Literal["foo"]:foo = "foo"return foobar = "bar" foo(bar)Here are …