python - 在两个文件中记录相同的东西扭曲
问题描述:
我想知道是否有方法在两个文件中以相同的方式记录相同的东西。 假设这是代码,现在我想将相同的输出转到“logs.log”以重定向到sys.stdout
。python - 在两个文件中记录相同的东西扭曲
if __name__ == "__main__":
log.startLogging(open("logs.log", 'a'))
log.startLogging(sys.stdout)
答
如果您使用Twisted的最新版本,这是绝对有可能的,比以往更容易。
from sys import stdout
from twisted.logger import Logger, textFileLogObserver, globalLogBeginner
# start the global logger
logfile = open('log.log', 'a')
globalLogBeginner.beginLoggingTo([
textFileLogObserver(stdout),
textFileLogObserver(logfile)])
log = Logger()
log.info('hello world')
log.debug('hello world')
您也可以implement you own logger,如果你想自定义消息。