0

I am writing a Python 3 application in which I want to log my messages to screen as well as a log file. I have set that up using logging handlers.

However, additionally, I also want to pipe the stdout and stderr output of any subprocess calls I make in the script to the same logging handlers (i.e both screen and log file).

I can do stdout=subprocess.PIPE, stderr=subprocess.STDOUT to capture the output in a variable and then log that variable. But that means nothing shows up on screen until that process is finished. However, I want the output to "stream" on the screen and in the log file, as the process is running.

Anyway to get this connection between subprocess output and logging handlers established in real-time?

martineau
  • 119,623
  • 25
  • 170
  • 301
shikhanshu
  • 1,466
  • 2
  • 16
  • 32
  • I believe you can do this by defining your own "pipe" class and using instances of it for the `stdout=` and `stderr=` arguments to the `subprocess` calls. An an example of doing something like that is in the `errorwindow3k.py` module shown in this [answer](https://stackoverflow.com/a/49016673/355230) of mine to another question. See the `OutputPipe` class it defines. In your case the class would need to write the data twice, once to `sys.stdout` or `sys.stderr`, and the other time to logger. – martineau Sep 14 '21 at 00:15
  • I think this is want you are looking for: [Calling python script with subprocess.Popen and flushing the data](https://stackoverflow.com/questions/14461843/calling-python-script-with-subprocess-popen-and-flushing-the-data) – Peter Trcka Sep 14 '21 at 12:30

0 Answers0