How to duplicate sys.stdout to a log file?

前端 未结 17 918
醉酒成梦
醉酒成梦 2020-11-22 06:54

Edit: Since it appears that there\'s either no solution, or I\'m doing something so non-standard that nobody knows - I\'ll revise my question to also ask: What is the best w

17条回答
  •  死守一世寂寞
    2020-11-22 07:25

    I know this question has been answered repeatedly, but for this I've taken the main answer from John T's answer and modified it so it contains the suggested flush and followed its linked revised version. I've also added the enter and exit as mentioned in cladmi's answer for use with the with statement. In addition, the documentation mentions to flush files using os.fsync() so I've added that as well. I don't know if you really need that but its there.

    import sys, os
    
    class Logger(object):
        "Lumberjack class - duplicates sys.stdout to a log file and it's okay"
        #source: https://stackoverflow.com/q/616645
        def __init__(self, filename="Red.Wood", mode="a", buff=0):
            self.stdout = sys.stdout
            self.file = open(filename, mode, buff)
            sys.stdout = self
    
        def __del__(self):
            self.close()
    
        def __enter__(self):
            pass
    
        def __exit__(self, *args):
            self.close()
    
        def write(self, message):
            self.stdout.write(message)
            self.file.write(message)
    
        def flush(self):
            self.stdout.flush()
            self.file.flush()
            os.fsync(self.file.fileno())
    
        def close(self):
            if self.stdout != None:
                sys.stdout = self.stdout
                self.stdout = None
    
            if self.file != None:
                self.file.close()
                self.file = None
    

    You can then use it

    with Logger('My_best_girlie_by_my.side'):
        print("we'd sing sing sing")
    

    or

    Log=Logger('Sleeps_all.night')
    print('works all day')
    Log.close()
    

提交回复
热议问题