0

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()

John Salzman
  • 500
  • 5
  • 14
  • Possible duplicate of [How to duplicate sys.stdout to a log file?](https://stackoverflow.com/questions/616645/how-to-duplicate-sys-stdout-to-a-log-file) – Green Cloak Guy Jun 25 '19 at 15:19
  • https://docs.python.org/3/library/logging.handlers.html – mad_ Jun 25 '19 at 15:20
  • @GreenCloakGuy that answer is not compatible on a Windows system – John Salzman Jun 25 '19 at 15:42
  • Nothing in your question mentions Windows. Please [edit] your question to elaborate on why the proposed duplicate in not suitable. – tripleee Jun 25 '19 at 15:50
  • 1
    Why not just log to file _and_ terminal? – hoefling Jun 25 '19 at 16:09
  • @hoefling Like use my logging function to log the message and also do a print() statement? There is many more commands that just those three up there and doing both would get sloppy. Also I don't think that would capture the output of the batch file. – John Salzman Jun 25 '19 at 16:13
  • 1
    Now you have changed the question and it's not clear anymore what the `logMessage` function is listed for. I'd suggest removing the fuzz, leaving only the related stuff. If it's about capturing the output of a subprocess, then it's surely possible with Python: https://stackoverflow.com/questions/4760215/running-shell-command-and-capturing-the-output – hoefling Jun 25 '19 at 16:30
  • Yea, sorry about that, trying to better convey what I'm trying to do. – John Salzman Jun 25 '19 at 16:33

1 Answers1

0

From this question on askubuntu:

command |& tee -a output.txt

Both the standard output and standard error streams will be copied to the file while still being visible in the terminal. If the file already exists, the new data will get appended to the end of the file.

So you could use python logEverything.py |& tee -a logFile.text

Community
  • 1
  • 1
benh
  • 194
  • 1
  • 9