2

I want to log the real time stdout from a different Class to a log file. Here is my demo code. Code works well. But it is waiting for the sample function to finish. I want to run it in parallel. So the log file will written in same time of stdout prints.

import sys
import time

class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("log.txt", "a")

    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)  

class sample:
  def __init__(self):
    print "TEST"
    time.sleep(5)

if __name__ == "__main__":
  a = sample()
  sys.stdout = Logger()
  • Did you try: `self.log.flush()`? after the `write`? Note that `sys.stdout` buffering can change depending on a number of factors/options, so you'd better `flush()` that as well. – Bakuriu Jun 03 '14 at 09:15
  • @Bakuriu ..Thanks its worked. Also I have to call `sys.stdout = Logger()` before `a = sample()` – Nitheesh Chandrika Jun 03 '14 at 09:25

1 Answers1

4

Calls to file.write do not necessarily write the contents to the HD immediately. It depends on the buffer policy of the file object. If you want to force writing to disk at a certain moment in time, you can use the flush() method (see also this).

Note that sys.stdout flushing policy depends on the configuration of your installation and also on environmental variables, so if you want to guarantee "parallel" writes between standard output and the log file you must flush() both streams:

def write(self, message):
    self.terminal.write(message)
    self.log.write(message)  
    self.terminal.flush()
    self.log.flush()
Community
  • 1
  • 1
Bakuriu
  • 98,325
  • 22
  • 197
  • 231