I am trying to log everything that passes through my command prompt. This includes batch script output that is launched by the python script.
If I run my python file like this
python logEverything.py >> logFile.txt
It gives me what I want and everything that is printed in the command prompt will instead be put into that file.
The problem with this is it no longer shows anything in the command prompt. Additionally I would like to be able to specify the logfile path inside of the program.
So is there a way to log everything that passes through the command prompt utilizing Python? Compatible for windows and Linux?
So logEverything.py would be something like this
import sys
def main():
import io
old_stdout = sys.stdout # Memorize the default stdout stream
sys.stdout = buffer = io.StringIO()
print('Log test')
os.system('this is a test')
print('More logging')
#Finished -> put everything into log_file.txt
sys.stdout = old_stdout
whatWasPrinted = buffer.getvalue()
print(whatWasPrinted) # Why not to print it?
buffer.close()
This solution partially works but it does not capture the output from the os.system()