python - log the same thing in two files twisted

為{幸葍}努か 提交于 2021-02-08 10:49:40

问题


I'd like to know if there's a way to log the same thing in two files in twisted. Let's say this is the code, now I'd like the same output going to "logs.log" to be redirected to sys.stdout.

if __name__ == "__main__":
    log.startLogging(open("logs.log", 'a'))
    log.startLogging(sys.stdout)

回答1:


This is absolutely possible and easier than ever before if you're on the latest version of 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')

You can also implement you own logger if you want custom messages.



来源:https://stackoverflow.com/questions/46641166/python-log-the-same-thing-in-two-files-twisted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!