登录到文件和控制台python

时间:2014-05-16 14:26:08

标签: python-2.7 logging

我正在使用Python的日志记录模块将一些调试字符串记录到一个给我良好日志的文件中 我使用附加的这个脚本,但它没有给我任何输出到屏幕 如何在屏幕上显示所有打印并使用formatt将其打印到日志中? 有什么帮助吗?

class StreamToLogger(object):
   """
   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())

logging.basicConfig(
   level=logging.DEBUG,
   format='%(asctime)s:%(levelname)s:%(name)s:%(message)s',
   filename="out.log",
   filemode='a'
)

stdout_logger = logging.getLogger('STDOUT')
sl = StreamToLogger(stdout_logger, logging.INFO)
sys.stdout = sl

#stderr_logger = logging.getLogger('STDERR')
#sl = StreamToLogger(stderr_logger, logging.ERROR)
#sys.stderr = sl

0 个答案:

没有答案