Haha TTpro
Haha TTpro

Reputation: 5546

Custom sys.stdout with flush()

I am trying to create a custom sys.stdout in my MeowLogTool Python logger

Here is the class.

class StreamToLogger(object):
   """
   Source: https://www.electricmonk.nl/log/2011/08/14/redirect-stdout-and-stderr-to-a-logger-in-python/
   Fake file-like stream object that redirects writes to a logger instance.
   """
   def __init__(self, logger, log_level=logging.INFO):
      self.logger = logger
      self.log_level = log_level
      self.linebuf = ''

   def write(self, buf):
      for line in buf.rstrip().splitlines():
         self.logger.log(self.log_level, line.rstrip())

However, sometime, I get this error about flush()

 'StreamToLogger' object has no attribute 'flush'

I don't know how to write flush() function for my specific case. I cannot find any example of custom sys.stdout with flush().

I would like to ask how to write flush()

Upvotes: 4

Views: 1656

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295403

Forward that to your logger's handlers:

def flush(self):
    for handler in self.logger.handlers:
        handler.flush()

Upvotes: 6

Related Questions