use*_*107 7 python logging stdout
我想使用Python将输出发送到log.txt终端上的文件和STDOUT.这是我有的:
import sys
class Logger(object):
def __init__(self, filename="Default.log"):
self.terminal = sys.stdout
self.log = open(filename, "a")
def write(self, message):
self.terminal.write(message)
self.log.write(message)
sys.stdout = Logger("log.txt")
print "Hello world !" #This line is saved in log.txt and STDOUT
Run Code Online (Sandbox Code Playgroud)
该程序将输出发送到文件和stdout.我的问题是:如何调用文件的写入函数?