On 26 Nov 2021, at 16:00, eyalgruss@gmail.com wrote:
i wonder whether:
from myutils import myprint as print
or
_print = print print = myprint
is really the pythonic way?
The problem with this is that there are more ways to output then the print command. Only replacing print will not catch any code that uses sys.stdout.write() for example. That is why when I need this I replace sys.stdout and usually also sys.stderr with a file like object. BTW did you know that you can get the original sys.stdout is also available in sys.__stdout__?
my use case for multiple files on top of the stdout, is when using e.g. wandb which is a popular ML dashboard and experiment logging platform. i want to write my log file both the a local log.txt and to a second copy in the temporary local wandb folder that later gets synced to the cloud. otherwise i have to take care of copying over the file later including in cases of exceptions. more generally: writing a log to both a local and a remote location.
As has been already suggested, you can use the logging module and add handlers for each of the log files that you want to create at the same time, I have also replaced sys.stdout with an object that takes all lines written to sys.stdout and sends them to the logger. Barry
thanks eyal _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/PEH5SG... Code of Conduct: http://python.org/psf/codeofconduct/