Python 带有flush()的自定义sys.stdout

Python 带有flush()的自定义sys.stdout,python,Python,我正在尝试在Python记录器中创建自定义sys.stdout 这是课堂 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 instan

我正在尝试在Python记录器中创建自定义sys.stdout

这是课堂

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())
但是,有时,我会遇到关于flush()的错误

我不知道如何为我的具体情况编写flush()函数。我找不到任何带有flush()的自定义sys.stdout示例


我想问一下如何编写flush()

并将其转发给记录器的处理程序:

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