I would like to create a function that keeps a record of every print command, storing each command’s string into a new line in a file.
def log(line):
with open('file.txt', "a") as f:
f.write('\n' + line)
This is what I have, but is there any way to do what I said using Python?
Try replacing
stdoutwith custom class:This would affect not only
print, but also any other function that prints something tostdout, but it is often even better.For production-mode logging it’s much better to use something like
loggingmodule, rather than home-made hooks over standard IO streams.