Python打印终端日志到TXT

Python打印终端日志到TXT

  • Logger
    • 代码如下:(有时间再详细解析2021.04.13)

Logger

代码如下:(有时间再详细解析2021.04.13)

放在程序开头就可以了

import sys
import os
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)
    def flush(self):
        pass
type = sys.getfilesystemencoding()
sys.stdout = Logger("left_process.txt")

你可能感兴趣的:(Python,python,logging)