in access/utils/helpers.py [0:0]
def log_stdout(filepath, mute_stdout=False):
'''Context manager to write both to stdout and to a file'''
class MultipleStreamsWriter:
def __init__(self, streams):
self.streams = streams
def write(self, message):
for stream in self.streams:
stream.write(message)
def flush(self):
for stream in self.streams:
stream.flush()
save_stdout = sys.stdout
log_file = open(filepath, 'w')
if mute_stdout:
sys.stdout = MultipleStreamsWriter([log_file]) # Write to file only
else:
sys.stdout = MultipleStreamsWriter([save_stdout, log_file]) # Write to both stdout and file
try:
yield
finally:
sys.stdout = save_stdout
log_file.close()