I have a long python script that uses print statements often, I was wondering if it was possible to add some code that would log all of the print statements into a text file
Here is a program that does what you describe:
#! /usr/bin/python3
class Tee:
def write(self, *args, **kwargs):
self.out1.write(*args, **kwargs)
self.out2.write(*args, **kwargs)
def __init__(self, out1, out2):
self.out1 = out1
self.out2 = out2
import sys
sys.stdout = Tee(open("/tmp/log.txt", "w"), sys.stdout)
print("hello")