【发布时间】:2016-07-05 06:58:48
【问题描述】:
这是我的示例代码
class StreamToLogger(object):
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="logger.log",
filemode='w'
)
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
suite = suite()
tests = unittest.TextTestRunner(descriptions=True, verbosity=2).run(suite)
作为TextTestRunner 默认使用流作为stderr。我已经使用 stderr 将输出写入日志文件并且它正在工作。我们可以在同一个脚本中多次使用stderr吗?
【问题讨论】:
标签: python stdout stderr python-unittest